NullReferenceException on DataGridView.Columns Index property

K

Kathy

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

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

rowe_newsgroups

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

Thanks,

Seth Rowe
 
K

Kathy

rowe_newsgroups said:
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.
 
R

rowe_newsgroups

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
 
K

Kathy

rowe_newsgroups said:
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
 
K

Kathy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top