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

前端 未结 4 1955
无人及你
无人及你 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 11:56

    First load data into a DataTable:

    var connection = @"Your connection string";
    var command = "Your SELECT command text";
    var table = new DataTable();
    using (var adapter = new SqlDataAdapter(command, connection))
        adapter.Fill(table);
    

    To show list of columns in a ComboBox:

    comboBox1.DataSource = table.Columns.Cast<DataColumn>().ToList();
    comboBox1.ValueMember = "ColumnName";
    comboBox1.DisplayMember = "ColumnName";
    

    To show data in DataGridView:

    dataGridView1.DataSource = table;
    

    In above code I suppose you are going to show columns of the table which you also want to load its data at the same time. In case which you just want to load just column information, you can use:

    adapter.FillSchema(table, SchemaType.Mapped); 
    
    0 讨论(0)
  • 2021-01-29 12:08

    actually your not using your DataReader but you just add Code, Model and ItemDescription item for each row found with the MySQL query.

    cbox_order.Items.Add("Code").ToString();
    cbox_order.Items.Add("Model").ToString();
    cbox_order.Items.Add("Itemdescription").ToString();
    

    If you want to use the result of the MySQL query you can try this instead:

    cbox_order.Items.Add(reader["Code"].ToString()).ToString(); // Change "Code" by the column name into the database
    cbox_order.Items.Add(reader["Model"].ToString()).ToString(); // Change "Model" by the column name into the database
    cbox_order.Items.Add(reader["Itemdescription"].ToString()).ToString(); // Change "Itemdescription" by the column name into the database
    

    Don't forget to close the reader at the end

    reader.Close();
    

    EDIT

    if you want the column name instead of data you can use this query, but that's useless if you already know the column name.

    SELECT COLUMN_NAME FROM information_schema.columns WHERE table_schema='databasename' AND table_name='tablename'
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2021-01-29 12:14

    Try to close and dispose of reader and close the connection.

    reader.close;
    reader.dispose;
    
    con.close();
    
    0 讨论(0)
提交回复
热议问题