How to display column name of my table (database) into combobox?
Asked
Active
Viewed 635 times
-2
-
if you are using datatable just get the datatable and you should be able to reach its columns. so its name like `dtbl.Columns[0].Name` – Halil İbrahim Dec 28 '18 at 09:38
-
and you ware unable to retrieve column name or list in combobox box ? – parlad Dec 28 '18 at 09:43
-
@parladneupane column name there are multiple columns in table. i need to bind it into combo box – Indrajeet Kumar Dec 28 '18 at 09:47
-
well, you can access multiple columns, Did you get a list of the column first? – parlad Dec 28 '18 at 09:49
-
@parladneupane yes – Indrajeet Kumar Dec 28 '18 at 09:50
-
https://www.c-sharpcorner.com/UploadFile/mahesh/combobox-in-C-Sharp/ will help or google it. There is plenty of examples. – parlad Dec 28 '18 at 09:53
-
https://stackoverflow.com/a/52370379/3110834 – Reza Aghaei Dec 28 '18 at 11:07
1 Answers
3
You can try the following to bind the column name into combo box. Here I have used INFORMATION_SCHEMA.COLUMNS
to get the column name.
public Form1()
{
InitializeComponent();
BindColumnnameToComboBox();
}
public void BindColumnnameToComboBox()
{
DataRow dr;
SqlConnection con = new SqlConnection(@"Data Source=NiluNilesh;Initial Catalog=mynewdata;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS i where i.TABLE_NAME = 'Mark'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dr = dt.NewRow();
dr.ItemArray = new object[] { 0, "--Select--" };
dt.Rows.InsertAt(dr, 0);
comboBox1.ValueMember = "COLUMN_NAME";
comboBox1.DisplayMember = "COLUMN_NAME";
comboBox1.DataSource = dt;
con.Close();
}

Suraj Kumar
- 5,547
- 8
- 20
- 42
-
-
It is automatically crated to create and define user interface, controls in windows application. – Suraj Kumar Dec 28 '18 at 09:55
-
-
Yes in windows application it work as bootstrapping- https://stackoverflow.com/questions/12297079/very-simple-definition-of-initializecomponent-method – Suraj Kumar Dec 28 '18 at 09:58
-
1
-
[Set hint or watermark or default text for ComboBox without adding it as item](https://stackoverflow.com/a/50972125/3110834) – Reza Aghaei Dec 28 '18 at 11:08
-
[C# Filling Combo box with Column name of Database not Column values](https://stackoverflow.com/a/52370379/3110834) Without need to another query. – Reza Aghaei Dec 28 '18 at 11:10
-