如何在VB.Net中的DataGridView中添加记录? [英] How do I add records to a DataGridView in VB.Net?

查看:1998
本文介绍了如何在VB.Net中的DataGridView中添加记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我不使用数据集或数据库绑定。我有一个带有3个字段的小表单,当用户点击OK时,他们应该被添加到DataGridView控件中作为一个新行。

解决方案

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)

这篇关于如何在VB.Net中的DataGridView中添加记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆