-1
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);
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
psk
  • 15
  • 1
  • 7

2 Answers2

0

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"))
Jeff Fritz
  • 9,821
  • 7
  • 42
  • 52
  • when I debug, it always shows index value = -1 if (dataGridView1.Columns.IndexOf(combo)<0){ dataGridView1.Columns.Add(combo);} – psk Sep 08 '14 at 14:54
  • Right... your combobox that you are testing with is not yet added to the grid. I'll add a sample test to my answer – Jeff Fritz Sep 08 '14 at 14:57
  • Thanks for the response. If it is not getting added to the grid, why does it appear in grid when I run and correctly populate the values from database? – psk Sep 08 '14 at 15:00
  • When you create the column with new DataGridViewComboBoxColumn() it is not part of the grid at that line of code. When you test if it has been added, the new column has not yet been added, even though there are other columns with similar features in the grid. – Jeff Fritz Sep 08 '14 at 15:12
  • hmm ohk.. Could you please suggest something to fix this? – psk Sep 08 '14 at 15:17
  • I have updated my answer to reflect the changes I am recommending – Jeff Fritz Sep 08 '14 at 15:17
0

I'm not sure about the textbox, but everytime you click, a new DataGridViewComboBoxColumn is added to the grid. If you click 5 times, you'll see 5 DataGridViewComboBoxColumns with the dropdown being the result of the sql query.

You would need to check if the DataGridViewComboBoxColumns combo was already added:

if (dataGridView1.Columns.IndexOf(combo) < 0)
{
   // Add
}
Joe_Hendricks
  • 746
  • 4
  • 16
  • on pressing enter key or btn submit the number of comboboxes keep on increasing. – psk Sep 05 '14 at 18:23
  • if (dataGridView1.Columns.IndexOf(combo) < 0) { dataGridView1.Columns.Add(combo); } } – psk Sep 05 '14 at 18:25
  • If you add a breakpoint to `dataGridView1.Columns.IndexOf(combo)` before `dataGridView1.Columns.Add(combo);`, what value do you get? – Joe_Hendricks Sep 05 '14 at 18:25
  • I debugged by adding a breakpoint there. But it always shows index = -1 – psk Sep 08 '14 at 14:34