469,934 Members | 1,907 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

NullReferenceException on DataGridView.Columns Index property

I am trying to set a tooltip for a column cell in a data grid as
documented in the MS Visual Studio 2005 documentation. I set up a test
to match the example exactly including database column, data grid
column and data values. I also have a tooltip control on the form. If
I comment out the 'if' test, it works fine, but uncommented, I get a
NullReferenceException error on the
Me.dataGridView1.Columns("Rating").Index reference. Here is the code
snippet from the help doc:

' Sets the ToolTip text for cells in the Rating column.
Sub dataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles dataGridView1.CellFormatting

If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _
AndAlso Not (e.Value Is Nothing) Then

With Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIn dex)

If e.Value.Equals("*") Then
.ToolTipText = "very bad"
ElseIf e.Value.Equals("**") Then
.ToolTipText = "bad"
ElseIf e.Value.Equals("***") Then
.ToolTipText = "good"
ElseIf e.Value.Equals("****") Then
.ToolTipText = "very good"
End If

End With

End If

End Sub 'dataGridView1_CellFormatting
I'm new to vb.net, so be kind :-)

Thanks for the help.

Sep 13 '06 #1
5 4658
Well, lets check the simple cause first. Does the column named "Rating"
exist when this code executes?

Thanks,

Seth Rowe

Kathy wrote:
I am trying to set a tooltip for a column cell in a data grid as
documented in the MS Visual Studio 2005 documentation. I set up a test
to match the example exactly including database column, data grid
column and data values. I also have a tooltip control on the form. If
I comment out the 'if' test, it works fine, but uncommented, I get a
NullReferenceException error on the
Me.dataGridView1.Columns("Rating").Index reference. Here is the code
snippet from the help doc:

' Sets the ToolTip text for cells in the Rating column.
Sub dataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles dataGridView1.CellFormatting

If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _
AndAlso Not (e.Value Is Nothing) Then

With Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIn dex)

If e.Value.Equals("*") Then
.ToolTipText = "very bad"
ElseIf e.Value.Equals("**") Then
.ToolTipText = "bad"
ElseIf e.Value.Equals("***") Then
.ToolTipText = "good"
ElseIf e.Value.Equals("****") Then
.ToolTipText = "very good"
End If

End With

End If

End Sub 'dataGridView1_CellFormatting
I'm new to vb.net, so be kind :-)

Thanks for the help.
Sep 14 '06 #2

rowe_newsgroups wrote:
Well, lets check the simple cause first. Does the column named "Rating"
exist when this code executes?

Thanks,

Seth Rowe
Yes it does. It is visible in the column properties within the
datagridview.

Sep 14 '06 #3
Are you adding the Rating Column with code or by using the "Add Column"
dialog in design view? Also what are you putting in for the Name
property and the Header Text property of the column? I used the
following in my form_load event to add the "Rating" column and
everything works fine.

DataGridView1.Columns.Add("Rating", "Rating")
Dim values() As String = {"*", "**", "***", "****"}
For i As Integer = 0 To 4
DataGridView1.Rows.Add(values(i))
Next i

Let me know what you find out.

Thanks,

Seth Rowe
Kathy wrote:
rowe_newsgroups wrote:
Well, lets check the simple cause first. Does the column named "Rating"
exist when this code executes?

Thanks,

Seth Rowe

Yes it does. It is visible in the column properties within the
datagridview.
Sep 14 '06 #4

rowe_newsgroups wrote:
Are you adding the Rating Column with code or by using the "Add Column"
dialog in design view? Also what are you putting in for the Name
property and the Header Text property of the column? I used the
following in my form_load event to add the "Rating" column and
everything works fine.

DataGridView1.Columns.Add("Rating", "Rating")
Dim values() As String = {"*", "**", "***", "****"}
For i As Integer = 0 To 4
DataGridView1.Rows.Add(values(i))
Next i

Let me know what you find out.

Thanks,

Seth Rowe

I did not add the column in code. If I go into Edit Columns now,
Rating is one of the columns in the list. AND its properties shows
Rating as the DataPropertyName as well as the HeaderText.

Thanks,
Kathy

Sep 14 '06 #5
I got it working by changing the following:

If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _

to:

Dim col as New DataGridViewColumn

col = Me.dataGridView1.Columns(e.ColumnIndex)

if col.DataPropertyName = "Rating" _
I guess Microsoft needs to correct their documentation ;)

Kathy

Sep 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Mitchell S. Honnert | last post: by
3 posts views Thread by Johnny E. Jensen | last post: by
8 posts views Thread by Brian Pelton | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
6 posts views Thread by Miro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.