NullReferenceException on DataGridView.Columns Index property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kathy

    #1

    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
    NullReferenceEx ception error on the
    Me.dataGridView 1.Columns("Rati ng").Index reference. Here is the code
    snippet from the help doc:

    ' Sets the ToolTip text for cells in the Rating column.
    Sub dataGridView1_C ellFormatting(B yVal sender As Object, _
    ByVal e As DataGridViewCel lFormattingEven tArgs) _
    Handles dataGridView1.C ellFormatting

    If e.ColumnIndex = Me.dataGridView 1.Columns("Rati ng").Index _
    AndAlso Not (e.Value Is Nothing) Then

    With Me.dataGridView 1.Rows(e.RowInd ex).Cells(e.Col umnIndex)

    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.

  • rowe_newsgroups

    #2
    Re: NullReferenceEx ception on DataGridView.Co lumns Index property

    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
    NullReferenceEx ception error on the
    Me.dataGridView 1.Columns("Rati ng").Index reference. Here is the code
    snippet from the help doc:
    >
    ' Sets the ToolTip text for cells in the Rating column.
    Sub dataGridView1_C ellFormatting(B yVal sender As Object, _
    ByVal e As DataGridViewCel lFormattingEven tArgs) _
    Handles dataGridView1.C ellFormatting
    >
    If e.ColumnIndex = Me.dataGridView 1.Columns("Rati ng").Index _
    AndAlso Not (e.Value Is Nothing) Then
    >
    With Me.dataGridView 1.Rows(e.RowInd ex).Cells(e.Col umnIndex)
    >
    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.

    Comment

    • Kathy

      #3
      Re: NullReferenceEx ception on DataGridView.Co lumns Index property


      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.

      Comment

      • rowe_newsgroups

        #4
        Re: NullReferenceEx ception on DataGridView.Co lumns Index property

        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.C olumns.Add("Rat ing", "Rating")
        Dim values() As String = {"*", "**", "***", "****"}
        For i As Integer = 0 To 4
        DataGridView1.R ows.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.

        Comment

        • Kathy

          #5
          Re: NullReferenceEx ception on DataGridView.Co lumns Index property


          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.C olumns.Add("Rat ing", "Rating")
          Dim values() As String = {"*", "**", "***", "****"}
          For i As Integer = 0 To 4
          DataGridView1.R ows.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 DataPropertyNam e as well as the HeaderText.

          Thanks,
          Kathy

          Comment

          • Kathy

            #6
            Re: NullReferenceEx ception on DataGridView.Co lumns Index property

            I got it working by changing the following:

            If e.ColumnIndex = Me.dataGridView 1.Columns("Rati ng").Index _

            to:

            Dim col as New DataGridViewCol umn

            col = Me.dataGridView 1.Columns(e.Col umnIndex)

            if col.DataPropert yName = "Rating" _


            I guess Microsoft needs to correct their documentation ;)

            Kathy

            Comment

            Working...