C# - 向数据集添加新行

时间:2011-04-24 09:25:01

标签: c# dataset

我有这段代码:

    DataRow myNewRow;
    myNewRow = hRAddNewDataSet.Vication.NewRow();
    myNewRow["EmployeeID"] = Convert.ToInt32(employeeIDTextBox.Text);
    myNewRow["VicationDate"] = vicationDateDateTimePicker.Value;
    myNewRow["VicationSubject"] = vicationSubjectTextBox.Text;
    myNewRow["VicationType"] = Convert.ToInt32(vicationTypeComboBox.SelectedValue);
    myNewRow["Time"] = Convert.ToInt32(timeTextBox.Text);
    myNewRow["VicationAs"] = Convert.ToInt32(vicationAsComboBox.SelectedValue);
    myNewRow["StatementNo"] = statementNoTextBox.Text;
    myNewRow["StatementDate"] = statementDateDateTimePicker.Value;
    myNewRow["Info"] = infoTextBox.Text;
    hRAddNewDataSet.Vication.Rows.Add(myNewRow);

当我运行此代码时,它会根据需要添加新行,但它也会更新当前行取决于

的值
vicationBindingSource.Position

我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

由于存在数据绑定,所有对控件绑定属性的更改都将写入数据源(通常在成功验证控件之后)

因此,当您输入新行的值时,实际上是在改变当前行的值

您应该使用AddNew()

BindingSource方法添加行