且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-21 21:03:52

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

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

如果要在一个小的位置插入行,请使用Rows集合的 Insert()方法(如GWLlosa所述)...

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

我知道你提到你没有进行数据绑定,但是如果您在项目中定义了一个强大类型的数据集,其中包含单个数据表,则可以使用它并获取一些很好的强类型的方法来做这些东西,而不是依赖网格方法...

  DataSet1.DataTable.Add行(1,John Doe,true)


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

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.

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

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)...

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)