DataGridViewRow save data when adding a new row C#

219 Views Asked by At

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)

0

There are 0 best solutions below