skip to Main Content

As of now i can only display the 1 row but i also want to add the other row in my dropdown
This is my code
enter image description here

This is my code in text field and value field

DropTeam.DataSource = dtt;
DropTeam.DataValueField = "TeamDescr"+"TeamCode";
DropTeam.DataTextField = "TeamDescr"+"TeamCode";
DropTeam.DataBind();

3

Answers


  1. You have to provide the table name in the Fill function and Fill expect a DataSet instead of DataTable so it should be like this sd.Fill(ds,"Branch")

    here how to use DataSet with DataTable

    Login or Signup to reply.
  2. You can use a while:

        string mainconn = ConfigurationManager.ConnectionStrings["Ref"].ConnectionString;
        string sqlqueryy = "select * from Branch";
        using (SqlConnection connection = new SqlConnection(mainconn))
        {
            SqlCommand command = new SqlCommand(sqlqueryy, connection);
            connection.Open();
            SqlDataReader read = command.ExecuteReader();
            while (read.Read())
                {
                    string TeamDescr= read["TeamDescr"].ToString();
                    string TeamCode= read["TeamCode"].ToString();
                    DropTeam.Items.Add(TeamCode + " - " + TeamDescr);
                };
            read.Close();
            connection.Close();
         }
    
    Login or Signup to reply.
  3. Well, a typical dropdown (combo) will let you select a row. Typical a combo box can let you choose one row for display, and then "usually" the PK row.

    So the PK row selection should REMAIN the PK row value, but for display? Unlike say a access combo, there is no multiple column support for more then one row.

    So, what you can do is simple concatenation of the additonal columns.

    Say we have drop down for hotel Name. We have say this:

    SELECT ID, HotelName from tblHotels ORDER BY HotelName
    

    To display say Hotel name and city in the drop?

    You could go like this:

    SELECT ID, HotelName + ',' + City As MyHotel from tblHotels ORDER BY HotelName
    

    The drop will will only return for you the one PK value (ID). From that value, you can then get/use/derive the actual city name from that combo.

    So markup:

            <h3>Select Hotel</h3>
            <asp:DropDownList ID="cboHotel2" runat="server"
                DataValueField="ID"
                DataTextField="MyHotel"  Width="288px"
                ></asp:DropDownList>
    

    code:

                string strSQL = "SELECT ID, HotelName, City, " +
                                "(HotelName + ' (' + City + ')') AS MyHotel " + 
                                "FROM tblHotels ORDER BY HotelName";
    
                cboHotel.DataSource = MyRst(strSQL);
                cboHotel.DataBind();
    

    output:

    enter image description here

    And you could add a fixed font size.
    (but the drop down HTML will remove those blanks).

    But, you can get around that issue by padding in ‘u00A0’ chars.

    So, lets adopt a fixed font, and do this:

            <asp:DropDownList ID="cboHotel" runat="server"
                DataValueField="ID"
                DataTextField="HotelName"  Width="288px"
                style="font-family:courier, 'courier new', monospace;"
                ></asp:DropDownList>
    

    And to fill, we do this:

                strSQL = "SELECT ID, HotelName, City " +
                         "FROM tblHotels ORDER BY HotelName";
    
                DataTable rst = MyRst(strSQL);
                rst.Columns[1].MaxLength = 255;
    
                foreach (DataRow MyRow in rst.Rows)
                {
                    MyRow["HotelName"] = ((String)MyRow["HotelName"]).PadRight(35, 'u00A0') +
                        " | " + MyRow["City"];
                }
                cboHotel.DataSource = rst;
                cboHotel.DataBind();
    

    So, we just pre-process the table data, and we now get this:

    enter image description here

    Now, any selected row will ONLY return the PK value, and then you can get the City name, tax rate or anything else from that combo selection.

    The 3rd way? You would "fake" the drop down, and pop down (display) a grid view with all the columns. But, I not posted that solution unless it comes to light that either of the above two ideas will not suffice.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search