Results 1 to 5 of 5

Thread: [RESOLVED] [2005] DataGridView Column Error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Resolved [RESOLVED] [2005] DataGridView Column Error

    Hi Guys,
    I've got this code which causes a Null Reference exception

    Code:
           If My.Computer.Keyboard.CtrlKeyDown Then
                If e.KeyCode = Keys.P Then
                    If Me.DGJobLines.Columns("ClmLinePrice").Visible Then <---Here
                        Me.DGJobLines.Columns("ClmLinePrice").Visible = False
                    Else
                        Me.DGJobLines.Columns("ClmLinePrice").Visible = True
                    End If
                End If
            End If
    If I use this code it works

    Code:
           If My.Computer.Keyboard.CtrlKeyDown Then
                If e.KeyCode = Keys.P Then
                    If Me.DGJobLines.Columns(2).Visible Then
                        Me.DGJobLines.Columns(2).Visible = False
                    Else
                        Me.DGJobLines.Columns(2).Visible = True
                    End If
                End If
            End If
    Here is how the column is added to the grid, and yes there are other columns as well.
    Code:
            Dim LinePriceCellStyle As New DataGridViewCellStyle
            With LinePriceCellStyle
                .Alignment = DataGridViewContentAlignment.MiddleRight
                .Format = "c2"
                .NullValue = "0.00"
            End With
    
            Dim ClmLinePrice As DataGridViewColumn = New DataGridViewTextBoxColumn
            With ClmLinePrice
                .Width = 50
                .HeaderText = "Price"
                .DefaultCellStyle = LinePriceCellStyle
                .Visible = False
            End With
            Me.DGJobLines.Columns.Add(ClmLinePrice)
    Any Ideas?
    Fishy

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] DataGridView Column Error

    Code:
    If My.Computer.Keyboard.CtrlKeyDown Then
                If e.KeyCode = Keys.P Then
                    If Me.DGJobLines.Columns("ClmLinePrice").Visible Then <---Here
                        Me.DGJobLines.Columns("ClmLinePrice").Visible = False
                    Else
                        Me.DGJobLines.Columns("ClmLinePrice").Visible = True
                    End If
                End If
            End If
    ClmLinePrice is the name of your column in memory, but not in the actual DataGridView. Try changing to this:

    Code:
    If My.Computer.Keyboard.CtrlKeyDown Then
                If e.KeyCode = Keys.P Then
                    If Me.DGJobLines.Columns("Price").Visible Then <---Here
                        Me.DGJobLines.Columns("Price").Visible = False
                    Else
                        Me.DGJobLines.Columns("Price").Visible = True
                    End If
                End If
            End If
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DataGridView Column Error

    The HeaderText is irrelevant. This:
    vb.net Code:
    1. If Me.DGJobLines.Columns("ClmLinePrice").Visible Then
    is trying to use a column whose Name is "ClmLinePrice". This:
    vb.net Code:
    1. Dim ClmLinePrice As DataGridViewColumn = New DataGridViewTextBoxColumn
    2.         With ClmLinePrice
    3.             .Width = 50
    4.             .HeaderText = "Price"
    5.             .DefaultCellStyle = LinePriceCellStyle
    6.             .Visible = False
    7.         End With
    8.         Me.DGJobLines.Columns.Add(ClmLinePrice)
    is adding a column with no Name at all. If you want the Name to be "ClmLinePrice" then you have to set the Name to "ClmLinePrice":
    Code:
            Dim ClmLinePrice As DataGridViewColumn = New DataGridViewTextBoxColumn
            With ClmLinePrice
                .Name = "ClmLinePrice"
                .Width = 50
                .HeaderText = "Price"
                .DefaultCellStyle = LinePriceCellStyle
                .Visible = False
            End With
            Me.DGJobLines.Columns.Add(ClmLinePrice)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] DataGridView Column Error

    Nice catch. I overlooked HeaderText while skimming for parenthesis.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: [2005] DataGridView Column Error

    Thanks jm, Could not see the wood for the trees then.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width