Click here to Skip to main content
15,068,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a one Gridview in C# and it has three columns. First column is a combobox column. I want to add data to the combobox column in datagrid from SQL Server 2005 database at runtime.

so how to do it? please help me.
Thanks in advance
Updated 31-May-10 1:18am
Sandeep Mewara 31-May-10 7:15am
It's a very common thing. Just Google it out. Even CodeProject search itself can give you few articles on the same.

Get the data from SQL Server 2005 in your normal way and then set the DataSource for the DataGridViewComboBoxColumn to the data you have retrieved.
Here is the code which i already used,

go through u may find something which u need.

DataGridViewComboBoxColumn comboBoxCol = new DataGridViewComboBoxColumn();
       DataGridViewComboBoxColumn comboBoxCol1 = new DataGridViewComboBoxColumn();
       DataGridViewTextBoxColumn comboBoxCol2 = new DataGridViewTextBoxColumn();
       //DataGridViewComboBoxColumn comboBoxCol2 = new DataGridViewComboBoxColumn();
       DataRow dr = null;
       private void FirstRow(int rc)
           //dt.Columns.Add(new DataColumn("Property", typeof(DataGridViewComboBoxColumn)));
           //dt.Columns.Add(new DataColumn("Operator", typeof(DataGridViewComboBoxColumn)));
           //dt.Columns.Add(new DataColumn("Value", typeof(string)));
           //dt.Columns.Add(new DataColumn("Options", typeof(string)));
           for (int i = 0; i < rc; i++)
               dr = dt.NewRow();
               //dr["Value"] = string.Empty;
               //dr["Options"] = string.Empty;
           dgridProperties.DataSource = dt;
           comboBoxCol.HeaderText = "Property";
           comboBoxCol.Name = "CmbProperty";
           dgridProperties.Columns.Insert(0, comboBoxCol);
           comboBoxCol1.HeaderText = "Operator";
           comboBoxCol1.Name = "CmbOperator";
           dgridProperties.Columns.Insert(1, comboBoxCol1);
           //ds = dc.FillGridview("select MpID,[Name] as 'Name' from morepropertiesgeneration");
           ds = dc.FillGridview("select ObjID,[ObjectName] as 'Name' from ObjectTypes");
           comboBoxCol2.HeaderText = "Value";
           comboBoxCol2.Name = "CmbValue";
           dgridProperties.Columns.Insert(2, comboBoxCol2);
           comboBoxCol.DataSource = ds.Tables[0];
           comboBoxCol.DisplayMember = "Name";
           comboBoxCol.ValueMember = "Name";
           //comboBoxCol.ValueMember = "MpID";

Henry Minute 1-Jun-10 11:34am
FYI: When you post code, please enclose it in <pre> </pre> tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900