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
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);