I am populating my datagrid based on a value in the textbox.
One of the field values in my grid view is a combobox.On a particular text box entry it shows me the correct results but wen I give another value in the text box, the combobox increases its number which means If I enter 100, the data is populated correctly in my combobox but for any value that is provided next the number of combobox becomes 2.I don't know why this happens. This is the code on button click. please help
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Supplier";
//execute sql data adapter to get supplier values
DataTable dt = obj.SqlDataTable("select name1 from blahblah");
//foreach (DataRow supplier in dt.DataSet.Tables[0].Rows)
//{
// combo.Items.Add(supplier[0]);
//}
//dataGridView1.Columns.Add(combo);
foreach (DataRow row in dt.Rows)
{
combo.Items.Add(row["NAME1"].ToString());
}
dataGridView1.Columns.Add(combo);
You're adding a new combobox on every click... instead check if the comboboxColumn is already added and appropriately modify its contents if it is already present.
To do this check, name your combobox column like this:
combo.Name = "Supplier";
the name property will not show in the UI, but Windows Forms will remember it. Then, you can test if your column is already added with:
if (!dataGridView1.Columns.Contains("Supplier"))