DataGridViewRow在添加新行C#时保存数据

时间:2015-05-08 07:39:28

标签: c# datagridview combobox datarelation

我在Win Form中有一个DataGridView,其中2个ComboBoxColumns彼此绑定。当用户从第一个组合框列中选择一个类别时,将过滤第二个组合的值。

当我添加第一行时代码运行良好,但是当我添加新行并且第1个组合框的值与第1行不同时,第1行的子组合框会更新并出现异常。

这是我的代码:

      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);

我需要在用户添加新行时立即保存上一行的更改。帮助,请)

0 个答案:

没有答案
相关问题