以编程方式将行添加到DataGridTable

时间:2021-06-27 14:43:55

How do I add rows programmatically to a DataGridTable in C#? When the user selects an option, I want to repopulate a DataGridTable with fresh data, which I am acquiring as shown below:

如何以编程方式将行添加到C#中的DataGridTable?当用户选择一个选项时,我想用新数据重新填充DataGridTable,我将获取该数据,如下所示:

        connect.Open();
        MySqlCommand comm = connect.CreateCommand();
        comm.CommandText = getCustomerInvoices + customerID + "\'";
        MySqlDataReader r = comm.ExecuteReader();
        while (r.Read())
        {
            DataGridViewRow d = new DataGridViewRow();
            String[] parameters = new String[5];
            parameters[0] = r.GetValue(0).ToString();
            parameters[1] = r.GetValue(1).ToString();
            parameters[2] = r.GetValue(2).ToString();
            parameters[3] = r.GetValue(3).ToString();
            parameters[4] = r.GetValue(4).ToString();
            d.SetValues(parameters);
            invoiceTable.Rows.Add(d);
        }
        connect.Close();

What seems to happen is that I get a new row added to the table, but the old rows are still there, and the new row does not appear to have any values in it (a bunch of blank textboxes, and I know that this query is returning one result in my test case).

似乎发生的事情是我在表中添加了一个新行,但旧行仍然存在,并且新行似乎没有任何值(一堆空白文本框,我知道这个查询在我的测试用例中返回一个结果)。

Can someone explain to me what I have to do?

有人可以向我解释一下我要做什么吗?

2 个解决方案

#1


I believe Rows.Add() has an overload for an array of strings. If you are creating an array of strings anyways, why not try using it? i.e. instead of:

我相信Rows.Add()有一个字符串数组的重载。如果你正在创建一个字符串数组,为什么不尝试使用它?即代替:

d.SetValues(parameters);
invoiceTable.Rows.Add(d);

you would juse use:

你会juse使用:

invoiceTable.Rows.Add(parameters);

Alternatively, if you still want to create a DataGridViewRow object and call setvalues on it, I think you need to either cast the array to an array of type Object, or pass each value as it's own argument instead of passing the array.

或者,如果你仍然想要创建一个DataGridViewRow对象并在其上调用setvalues,我认为你需要将数组转换为Object类型的数组,或者将每个值作为它自己的参数传递而不是传递数组。

#2


I believe you need to call .CreateCells(GridName) before you call setvalue

我相信你需要在调用setvalue之前调用.CreateCells(GridName)

#1


I believe Rows.Add() has an overload for an array of strings. If you are creating an array of strings anyways, why not try using it? i.e. instead of:

我相信Rows.Add()有一个字符串数组的重载。如果你正在创建一个字符串数组,为什么不尝试使用它?即代替:

d.SetValues(parameters);
invoiceTable.Rows.Add(d);

you would juse use:

你会juse使用:

invoiceTable.Rows.Add(parameters);

Alternatively, if you still want to create a DataGridViewRow object and call setvalues on it, I think you need to either cast the array to an array of type Object, or pass each value as it's own argument instead of passing the array.

或者,如果你仍然想要创建一个DataGridViewRow对象并在其上调用setvalues,我认为你需要将数组转换为Object类型的数组,或者将每个值作为它自己的参数传递而不是传递数组。

#2


I believe you need to call .CreateCells(GridName) before you call setvalue

我相信你需要在调用setvalue之前调用.CreateCells(GridName)