在 vb.net 中向 datagridview 添加行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24679284/
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-17 17:50:03  来源:igfitidea点击:

Add row to datagridview in vb.net

vb.netdata-bindingdatagridview

提问by user3388946

When I try add an extra row to my datagridview I get following error:

当我尝试向 datagridview 添加额外的行时,出现以下错误:

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

当控件是数据绑定的时,不能以编程方式将行添加到 DataGridView 的行集合中。

Any idea to fix this, without databinding I added rows like this:

任何解决这个问题的想法,没有数据绑定我添加了这样的行:

    ' Populate the rows.
    Dim row() As String = {omschrijving, aantalstr, eenheidsprijs, basisbedrag, kortingstr, kortingbedrag, netto, btw, btwbedrag, totaal, productid}

    DataGridView1.Rows.Add(row)

回答by Anthony

It looks like your grid view is bound to a data object. In that case, you need to add the row to the object it is bound to, like a dataset.

看起来您的网格视图绑定到数据对象。在这种情况下,您需要将该行添加到它绑定到的对象中,例如数据集。

For instance, a rough example would be:

例如,一个粗略的例子是:

Dim boundSet As New DataSet

Dim newRow As DataRow = boundSet.Tables(0).NewRow
With newRow
    .Item(0) = "omschrijving"
    .Item(1) = "aantalstr"
    ...
End With

boundSet.Tables(0).Rows.Add(newRow)
boundSet.AcceptChanges()

You would just need to use the dataset that was bound to your grid view instead of creating a new one.

您只需要使用绑定到您的网格视图的数据集,而不是创建一个新的数据集。