1

I have a DataGridView in Win Form with 2 ComboBoxColumns bound to each other. When a user chooses a category from first combobox column, values of the second one are filtered.

When I add the first row the code works well, but when I add new row and value of 1st combobox is different from the 1st row, child combobox of 1st row is updated and exception arises.

Here is my code:

      DataGridViewComboBoxColumn specCmb = new DataGridViewComboBoxColumn();
            specCmb.HeaderText = "Specification";
            specCmb.DataSource = specBndSource;
            specCmb.DisplayMember = "specName";
            dataGridView1.Columns.Add(specCmb);
            SqlDataAdapter itemAdapter = new SqlDataAdapter("SELECT ItemCode, ItemName, FrgnName, U_Index FROM dbo.OITM WHERE ItemCode LIKE 'AE%'", sqlConnection);
            itemAdapter.Fill(dataset, "DTF.dbo.OITM");
            DataRelation rel = new DataRelation("SpecItem", 
                dataset.Tables["DTF_Medical.dbo.Specification"].Columns["specID"],
                dataset.Tables["DTF.dbo.OITM"].Columns["U_Index"]);
            dataset.Relations.Add(rel);

    DataGridViewComboBoxColumn itemCodeCmb = new DataGridViewComboBoxColumn();
            BindingSource itemBndSource = new BindingSource();
            itemBndSource.DataSource = specBndSource;
            itemBndSource.DataMember = "SpecItem";
            itemCodeCmb.HeaderText = "Item Code";
            itemCodeCmb.DataSource = itemBndSource;
            itemCodeCmb.DisplayMember = "ItemCode";
            itemCodeCmb.ValueMember = "ItemCode";
            dataGridView1.Columns.Add(itemCodeCmb);

I need to save changes of the previous row as soon as user adds a new one. Help,please)

Diana
  • 11
  • 2
  • This is because they are sharing the specBndSource...You will have to do some more work to get the behavior you want. You need two different BindingSources and you should probably capture the CellValueChanged event on the 1st to update the 2nd – waltmagic May 08 '15 at 16:19
  • @waltmagic I need to make DataRelation, that;s why they both share the same Binding Source. If I make 2 different Binding Sources, how can I make a relation between them? Thatnks a lot for ur answer) – Diana May 11 '15 at 07:11
  • I will try to write a quick coding example to show you how in a little while – waltmagic May 11 '15 at 15:32
  • My attempt to get an example working failed...Forget what I said about two separate BindingSources...I am not sure this is possible using ComboBoxColumn alone. I say this because every new row is going to be sharing a common BindingSource with all the other rows no matter what. I did find a link that should get you going in the right direction using ComboBoxCell. Try this link here --> https://social.msdn.microsoft.com/Forums/en-US/8f8612ce-e467-463b-851c-a9084541fbf9/different-data-sources-in-datagridviewcomboboxcolumn-for-each-row?forum=winformsdatacontrols – waltmagic May 11 '15 at 17:34

0 Answers0