请参阅以下代码:
Private Sub DataGridViewBorrowedBooks_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridViewBorrowedBooks.SelectionChanged
'DataGridViewBookDetails.DataSource = Nothing
If DataGridViewBorrowedBooks.SelectedRows.Count = 0 Then
Return
End If
Dim row = DataGridViewBorrowedBooks.SelectedRows(0)
If row Is Nothing Then
Return
End If
Dim isbn As Decimal = Integer.Parse(row.Cells("isbn").Value.ToString())
' Get member record from treeview control.
Dim member = TryCast(TreeViewMembers.SelectedNode.Tag, MemberModel)
' Check for root node. It does not have a member record
If member Is Nothing Then
Return
End If
' Locate order record
For Each loan In member.Loan
If loan.ISBN = isbn Then
DataGridViewBookDetails.DataSource = loan.Books
DataGridViewBookDetails.Columns("ISBN").Visible = False
DataGridViewBookDetails.Columns("Title").DefaultCellStyle.Format = "C"
DataGridViewBookDetails.Columns("Author").DefaultCellStyle.Format = "C"
DataGridViewBookDetails.Columns("Price").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
Return
End If
Next loan
End Sub
我得到的异常是:System.NullReferenceException,位于以下行:'DataGridViewBookDetails.Columns("ISBN").Visible = False
答案 0 :(得分:0)
这里的问题是,"ISBN"
集合中没有名称为Columns
的列。您需要将其更改为列的实际名称,或者如果更合适,则使用索引。要查找所有列名称,您只需将其打印出来即可。它很可能映射到一个名字,一旦你看到它就会非常明显
For Each column in DataGridViewBookDetails.Columns
MessageBox.Show(column.Name)
Next