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

前端 未结 4 1963
无人及你
无人及你 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().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); 
    

提交回复
热议问题