添加新行时 dataGridview 列中的默认值出现问题

发布于 2024-12-05 16:32:15 字数 507 浏览 0 评论 0原文

我有一个带有 dataGridView 的表单 (form1),其中列出了名为“Orders”的表中的客户订单。当在 dataGridView 中选择订单并按下 Enter 键时,会弹出另一个表单 (form2)。此弹出 form2 有另一个 dataGridView,支持添加行,用户在其中将与所选订单相关的新记录输入到名为“OrderDetails”的表中,该表具有名为 OrderNumber 的外键列,该列引用 OrderNumber 列在表“订单”中。

问题:

当用户从 form2 上的 dataGridView 输入新记录到 orderDetails 表中时,我希望自动填充 OrderNumber 列。它的值应该是 form1 上所选记录的订单号值,这样用户就不必手动键入它,因为它是已知的。帮助。

datagridview 绑定到 bidningSource。

I have a form (form1) with a dataGridView listing customer orders from a table called "Orders". When an order is selected in the dataGridView and the EnterKey is pressed, another form (form2) pops-up. This popup form2 has another dataGridView that supports adding rows where the user enters a new record related to the selected order into a table called "OrderDetails" which has a foreigh key column called OrderNumber which refers to the OrderNumber column in table "Orders".

Problem:

when the user is entering a new record from the dataGridView on form2 into the orderDetails table, i want the OrderNumber column to be populated automatically. Its value should be the order number value of the selected record on form1 such that the user doesn't have to type it manually since its already known. Help.

The datagridview is bound to a bidningSource.

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

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

发布评论

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

评论(2

折戟 2024-12-12 16:32:15

刚刚让它按照我想要的方式工作

private void dataGridView1_DefaultValuesNeeded(object sender,
    System.Windows.Forms.DataGridViewRowEventArgs e)
{

  e.Row.Cells["CustomerNumber"].Value = myOrderNumberValue;

}

just got it working the way i wanted with

private void dataGridView1_DefaultValuesNeeded(object sender,
    System.Windows.Forms.DataGridViewRowEventArgs e)
{

  e.Row.Cells["CustomerNumber"].Value = myOrderNumberValue;

}
青萝楚歌 2024-12-12 16:32:15

您可以捕获 BindingSource 上的一个名为“AddingNew”的事件。您可以在此处指定新对象及其属性。像这样:

private void BindingSource_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
{
    OrderDetailLIne odl = new OrderDetailLIne();
    odl.OrderNumber = _orderNumber;
    e.NewObject = odl;
}

There's an event on a BindingSource you can catch called 'AddingNew'. Here you can specify what the new object and its properties will be. Like this:

private void BindingSource_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
{
    OrderDetailLIne odl = new OrderDetailLIne();
    odl.OrderNumber = _orderNumber;
    e.NewObject = odl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文