如何将记录添加到 VB.Net 中的 DataGridView?

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

How do I add records to a DataGridView in VB.Net?

vb.netdatagridview

提问by codeConcussion

How do I add new record to DataGridView control in VB.Net?

如何在 VB.Net 中向 DataGridView 控件添加新记录?

I don't use dataset or database binding. I have a small form with 3 fields and when the user clicks OK they should be added to the DataGridView control as a new row.

我不使用数据集或数据库绑定。我有一个包含 3 个字段的小表单,当用户单击“确定”时,它们应该作为新行添加到 DataGridView 控件中。

回答by codeConcussion

If you want to add the row to the end of the grid use the Add()method of the Rows collection...

如果要将行添加到网格的末尾,请使用Rows 集合的Add()方法...

DataGridView1.Rows.Add(New String(){Value1, Value2, Value3})

If you want to insert the row at a partiular position use the Insert()method of the Rows collection (as GWLlosa also said)...

如果要在特定位置插入行,请使用Rows 集合的Insert()方法(正如 GWLlosa 所说)...

DataGridView1.Rows.Insert(rowPosition, New String(){value1, value2, value3})

I know you mentioned you weren't doing databinding, but if you defined a strongly-typed dataset with a single datatable in your project, you could use that and get some nice strongly typed methods to do this stuff rather than rely on the grid methods...

我知道你提到你没有做数据绑定,但是如果你在你的项目中定义了一个带有单个数据表的强类型数据集,你可以使用它并获得一些很好的强类型方法来做这些事情,而不是依赖网格方法...

DataSet1.DataTable.AddRow(1, "John Doe", true)

回答by Galwegian

I think you should build a dataset/datatable in code and bind the grid to that.

我认为你应该在代码中构建一个数据集/数据表并将网格绑定到它。

回答by Mr.Buntha Khin

When I try to cast data source from datagridview that used bindingsource it error accor cannot casting:

当我尝试从使用 bindingsource 的 datagridview 转换数据源时,它错误accor无法转换:

----------Solution------------

- - - - - 解决方案 - - - - - -

'I changed casting from bindingsource that bind with datagridview

'我改变了从绑定源绑定到数据网格视图的转换

'Code here

'代码在这里

Dim dtdata As New DataTable()

dtdata = CType(bndsData.DataSource, DataTable)

回答by GWLlosa

The function you're looking for is 'Insert'. It takes as its parameters the index you want to insert at, and an array of values to use for the new row values. Typical usage might include:

您正在寻找的功能是“插入”。它将您要插入的索引和用于新行值的值数组作为其参数。典型用法可能包括:

myDataGridView.Rows.Insert(4,new object[]{value1,value2,value3});

or something to that effect.

或类似的东西。

回答by cjbarth

If you want to use something that is more descriptive than a dumb array without resorting to using a DataSet then the following might prove useful. It still isn't strongly-typed, but at least it is checked by the compiler and will handle being refactored quite well.

如果您想使用比哑数组更具描述性的内容而不诉诸使用 DataSet,那么以下内容可能会很有用。它仍然不是强类型的,但至少它由编译器检查并且可以很好地处理重构。

Dim previousAllowUserToAddRows = dgvHistoricalInfo.AllowUserToAddRows
dgvHistoricalInfo.AllowUserToAddRows = True

Dim newTimeRecord As DataGridViewRow = dgvHistoricalInfo.Rows(dgvHistoricalInfo.NewRowIndex).Clone

With record
    newTimeRecord.Cells(dgvcDate.Index).Value = .Date
    newTimeRecord.Cells(dgvcHours.Index).Value = .Hours
    newTimeRecord.Cells(dgvcRemarks.Index).Value = .Remarks
End With

dgvHistoricalInfo.Rows.Add(newTimeRecord)

dgvHistoricalInfo.AllowUserToAddRows = previousAllowUserToAddRows

It is worth noting that the user must have AllowUserToAddRowspermission or this won't work. That is why I store the existing value, set it to true, do my work, and then reset it to how it was.

值得注意的是,用户必须获得AllowUserToAddRows许可,否则这将不起作用。这就是为什么我存储现有值,将其设置为 true,完成我的工作,然后将其重置为原来的状态。

回答by Robert

If your DataGridViewis bound to a DataSet, you can not just add a new row in your DataGridViewdisplay. It will now work properly.

如果您DataGridView绑定到 a DataSet,则不能只在DataGridView显示中添加新行。它现在将正常工作。

Instead you should add the new row in the DataSetwith this code:

相反,您应该DataSet使用以下代码在 中添加新行:

BindingSource[Name].AddNew()

This code will also automatically add a new row in your DataGridViewdisplay.

此代码还将自动在您的DataGridView显示中添加一个新行。