C# - 将新行添加到数据集

发布于 2024-11-03 05:49:44 字数 862 浏览 2 评论 0原文

我有这段代码:

    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

如何解决此问题?

I have this code :

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

When I run this code, it will add new row as I want but it will also update the current row depends on the value of

vicationBindingSource.Position

How I can solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2024-11-10 05:49:44

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

因此当您输入新行的值时,您实际上正在更改当前行的值首先,

您应该使用 BindingSourceAddNew() 方法添加行

since there is a databinding all your changes to the bound properties of the controls will be written to the datasource (normally after successful validation of the control)

so when you enter the values of you new row, you are actually changing the values of the current row first

you should add your row by using the AddNew() method of your BindingSource

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文