如何获得DataGridView某一格的内容
怎么获得DataGridView某一格的内容?
我的程序有一个名为DataGridView1的DataGridView,我想在程序运行时在DataGridView1的某一格(比如最后一行的第3格)输入一个值,然后有MsgBox显示它。程序如下:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer, s As String
i = DataGridView1.Rows.Count
s = DataGridView1.Rows(i - 1).Cells.Item(2).Value.ToString
MsgBox(s)
End Sub
但程序错误提示“未将对象引用设置到对象的实例”?
------解决方案--------------------
出现“未将对象引用设置到对象的实例”是因为datagridview控件的AllowUserToAddRows属性默认为True,因此datagridview控件中的最后一行是新增记录用的,这行中单元格的内容一般为null值,除非你设置了默认值。
实际上你要显示的是已有的最后一条记录的第3格内容,代码如下:
MsgBox(Me.DataGridView1.Rows(DataGridView1.RowCount - 2).Cells(2).Value.ToString)
------------------------------------------
我的书《Visual Basic .NET 2005数据库编程技术与实例》已由人民邮电出版社出版
人民邮电出版社出版:http://www.ptpress.com.cn/books/Book_Information.asp?BID=16271
中国互动出版网:http://www.china-pub.com/computers/common/info.asp?id=35208
------------------------------------------
我的程序有一个名为DataGridView1的DataGridView,我想在程序运行时在DataGridView1的某一格(比如最后一行的第3格)输入一个值,然后有MsgBox显示它。程序如下:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer, s As String
i = DataGridView1.Rows.Count
s = DataGridView1.Rows(i - 1).Cells.Item(2).Value.ToString
MsgBox(s)
End Sub
但程序错误提示“未将对象引用设置到对象的实例”?
------解决方案--------------------
出现“未将对象引用设置到对象的实例”是因为datagridview控件的AllowUserToAddRows属性默认为True,因此datagridview控件中的最后一行是新增记录用的,这行中单元格的内容一般为null值,除非你设置了默认值。
实际上你要显示的是已有的最后一条记录的第3格内容,代码如下:
MsgBox(Me.DataGridView1.Rows(DataGridView1.RowCount - 2).Cells(2).Value.ToString)
------------------------------------------
我的书《Visual Basic .NET 2005数据库编程技术与实例》已由人民邮电出版社出版
人民邮电出版社出版:http://www.ptpress.com.cn/books/Book_Information.asp?BID=16271
中国互动出版网:http://www.china-pub.com/computers/common/info.asp?id=35208
------------------------------------------