C# Filling Combo box with Column name of Database not Column values

前端 未结 4 2020
无人及你
无人及你 2021-01-29 11:19

I know it\'s been asked many times and there\'s so many resources about this but believe me i tried those, Unfortunately same thing is always happen. I really don\'t know why my

4条回答
  •  太阳男子
    2021-01-29 12:11

    If you check the code, you are basically just adding the strings "Code", "Model" and "Itemdescription" to the combobox. I guess you want rather something like:

    while (reader.Read())
    {
       cbox_order.Items.Add($"{reader["Code"]} {reader["Model"]} {reader["Itemdescription"]}");
    }
    

    In this snippet I am using the reader to get values of the columns in the returned row from the DB and then displaying joining those values in a single string that is then added to the ComboBox as an item.

    Update

    If you know the column names, why not just do this?

    public void FillComboBox()
    {
        cbox_order.Items.Add("Code").ToString();
        cbox_order.Items.Add("Model").ToString();
        cbox_order.Items.Add("Itemdescription").ToString();
    }
    

提交回复
热议问题