vb.net 将行添加到数据网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16187692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:51:27  来源:igfitidea点击:

Adding row to datagridview

vb.netvisual-studio-2010

提问by Geo Paul

I have a datagridview which has 3 columns 1st and 2nd is textbox columns and 3rd one is link column. I try to add a new row using the command.

我有一个 datagridview,它有 3 列,第一列和第二列是文本框列,第三列是链接列。我尝试使用该命令添加新行。

STEPS.Rows.Add(New String() {stepNo, "This is description for step " & stepNo},"link")

But for the first time when I try to execute the above line it shows index was outside the bounds of the array. This doesn't happen when I am trying to insert the second row. Also this does not happen when I am having all the 3 columns as TextBoxs.

但是当我第一次尝试执行上面的行时,它显示index was outside the bounds of the array. 当我尝试插入第二行时不会发生这种情况。当我将所有 3 列都作为文本框时,这也不会发生。

Please help me.

请帮我。

回答by danywalls

You are trying to use step.rows.add method but steps is a gridview not a datatable. the datatable object contains a methods row.add that expect a datarow object, this example show add row into datatable , and link with a gridview.

您正在尝试使用 step.rows.add 方法,但步骤是 gridview 而不是数据表。datatable 对象包含一个需要 datarow 对象的方法 row.add,此示例显示将行添加到 datatable 中,并与 gridview 链接。

    Dim dt As New DataTable

    dt.Columns.Add("field1")
    dt.Columns.Add("field2")

    Dim row1 As DataRow = dt.NewRow
    row1.Item("field1") = "Hello"
    row1.Item("field2") = "World"
    Dim row2 As DataRow = dt.NewRow
    row2.Item("field1") = "Hello2"
    row2.Item("field2") = "World2"
    dt.Rows.Add(row1)
    dt.Rows.Add(row2)
    GridView1.DataSource = dt
    GridView1.DataBind()