C# - 2 个问题 - 引用 DataGridView_RowsAdded 中的单元格会导致 nullreference 错误;无法使用 DataGridView 数据更新数据库

发布于 2024-09-29 18:01:03 字数 1569 浏览 7 评论 0原文

我刚刚开始用 C# 编写客户应用程序,其中大部分使用 Visual Studio 设计模式。请记住,我昨天才开始学习 C# - 所以如果我犯了任何愚蠢的错误,请指出。

到目前为止我所拥有的

  • 数据库;
  • 链接到客户和代理表的数据集;
  • 绑定到 DataSet 的 BindingSource;
  • 绑定到 BindingSource 的 Customer 表的 CustomerBindingSource;
  • 一个 AgentBindingSource,它绑定到 BindingSource 的 Agent 表;
  • 绑定到 CustomerBindingSource 的 DataGridView,其中 AgentID 列是绑定到 AgentBindingSource 的组合框。

到目前为止,这工作正常。但是我有几个问题,其中之一在以下代码中...

代码

void grdCustomers_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    DataGridViewRow gvr = grdCustomers.Rows[e.RowIndex];
    String currID = gvr.Cells[0].Value.ToString();
    if (currID.Equals(""))
    {
        gvr.Cells[0].Value = "{id}";
    }
}
void button1_Click(object sender, EventArgs e) //Save button for when the user has finished editing.
{
    foreach (DataGridViewRow gvr in grdCustomers.Rows)
    {
        String currID = gvr.Cells[0].Value.ToString();
        if (currID.Equals("{id}"))
        {
            String g = Guid.NewGuid().ToString();
            gvr.Cells[0].Value = g;
        }
    }
    customerTableAdapter.Update(customerAppDS21.Customer);
}

问题

  1. 第四行在尝试运行时导致问题程序。也就是说,对象引用未设置为对象的实例。我认为这是由 gvr.Cells[0] 引起的,但我不知道为什么。在我看来,如果在行设置完成(包括添加所有单元格)时调用此函数,那么这是有意义的。

  2. 我无法让 DataGridView 将数据绑定到数据库。我希望用户能够编辑任何值,但是当他们添加一行时,直到单击“保存”按钮 (button1) 才会计算 id。

如果有人能帮助我解决这些问题,我将不胜感激。

问候,

理查德

I just started writing a customer application in C#, using Visual Studio design mode for most of it. Bear in mind I only started learning C# yesterday - so if I am making any stupid mistakes then please say so.

What I have so far

  • A database;
  • A DataSet which links to the Customer and Agent tables;
  • A BindingSource which binds to the DataSet;
  • A CustomerBindingSource which binds to the Customer table of the BindingSource;
  • An AgentBindingSource which binds to the Agent table of the BindingSource;
  • A DataGridView which binds to the CustomerBindingSource, of which the AgentID column is a combobox which binds to the AgentBindingSource.

So far this is working ok. However I have a couple of problems, one of which is in the following code...

The Code

void grdCustomers_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    DataGridViewRow gvr = grdCustomers.Rows[e.RowIndex];
    String currID = gvr.Cells[0].Value.ToString();
    if (currID.Equals(""))
    {
        gvr.Cells[0].Value = "{id}";
    }
}
void button1_Click(object sender, EventArgs e) //Save button for when the user has finished editing.
{
    foreach (DataGridViewRow gvr in grdCustomers.Rows)
    {
        String currID = gvr.Cells[0].Value.ToString();
        if (currID.Equals("{id}"))
        {
            String g = Guid.NewGuid().ToString();
            gvr.Cells[0].Value = g;
        }
    }
    customerTableAdapter.Update(customerAppDS21.Customer);
}

The Problems

  1. The 4th line is causing issues when trying to run the program. It is saying that the object reference is not set to an instance of an object. I think this is caused by the gvr.Cells[0], but I dont know why. In my mind it would make sense if this was called when the setup of a row is complete (including adding all the cells).

  2. I cannot get the DataGridView to bind the data to the database. I want the user to be able to edit any of the values, but when they add a row the id is not calculated until they click the Save button (button1).

I would greatly appreciate it if someone could help me out with either of these problems.

Regards,

Richard

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

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

发布评论

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

评论(3

提笔落墨 2024-10-06 18:01:03

它的“值”为空。单元格的默认值始终为空。这意味着您可能会在填充之前读取它,或者该列没有任何值。在尝试访问它之前检查它是否为空。如果为 null,则该单元格没有值。

it is the ”Value” which is null. The default value of a cell is always null. This means that you might read it before it has been populated or that column just don’t have any value. Check to see if it is null before you try to access it. If it is null that cell has no value.

挥剑断情 2024-10-06 18:01:03

根据我的经验,在 cellFormatted 事件处理程序中修改单元格的内容更容易。

Its my experience that modifying contents of a cell is easier in the cellFormatted event handler.

2024-10-06 18:01:03

似乎您添加了一个包含空值的新行,然后为其分配了数据。
尝试更改向网格添加行的方式

It seems that you add a new row with null values then you assign data to it.
Try to change the way you add row to grid

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