C# - 将新行添加到数据集
我有这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于存在数据绑定,您对控件绑定属性的所有更改都将写入数据源(通常在成功验证控件之后),
因此当您输入新行的值时,您实际上正在更改当前行的值首先,
您应该使用
BindingSource
的AddNew()
方法添加行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 yourBindingSource