Home All Groups Group Topic Archive Search About

NullReferenceException on DataGridView.Columns Index property

Author
13 Sep 2006 11:05 PM
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.

Author
14 Sep 2006 12:39 AM
rowe_newsgroups
Well, lets check the simple cause first. Does the column named "Rating"
exist when this code executes?

Thanks,

Seth Rowe

Kathy wrote:
Show quoteHide quote
> 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.
Author
14 Sep 2006 2:05 AM
Kathy
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.
Author
14 Sep 2006 1:45 PM
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


Kathy wrote:
Show quoteHide quote
> 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.
Author
14 Sep 2006 8:23 PM
Kathy
rowe_newsgroups wrote:
Show quoteHide quote
> 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
Author
14 Sep 2006 9:07 PM
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