none
Datagridview MouseClick() 'Object reference not set to an instance of an object error' RRS feed

  • Question

  • Hi, 

    I get 'Object reference not set to an instance of an object error',

    If I click the corner of datagridview.

     I tried with this to condition but it persist:

    If Dgw1.SelectedRows.Count = 0 Then Exit Sub
    
    If Dgw1.Rows.Count > 0 AndAlso Dgw1.CurrentRow IsNot Nothing Then

    So what is wrong here.

    Thank you.


    Friday, March 1, 2019 11:54 PM

Answers

  • Hello,

    What line is throwing the exception?

    I tested this with a project I had open, no errors.

    Private Sub DataGridView1_MouseClick(
        sender As Object,
        mouseEventArgs As MouseEventArgs) _
        Handles DataGridView1.MouseClick
    
        If DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
    
        If DataGridView1.Rows.Count > 0 AndAlso DataGridView1.CurrentRow IsNot Nothing Then
            Console.WriteLine("Here")
        End If
    
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, March 2, 2019 12:36 AM
    Moderator

All replies

  • Hello,

    What line is throwing the exception?

    I tested this with a project I had open, no errors.

    Private Sub DataGridView1_MouseClick(
        sender As Object,
        mouseEventArgs As MouseEventArgs) _
        Handles DataGridView1.MouseClick
    
        If DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
    
        If DataGridView1.Rows.Count > 0 AndAlso DataGridView1.CurrentRow IsNot Nothing Then
            Console.WriteLine("Here")
        End If
    
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, March 2, 2019 12:36 AM
    Moderator
  • Hi Bajtitou,

    When we click on the top-left corner of DataGridView, all cells will be selected.
    What do you want to do with clicking it?

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Saturday, March 2, 2019 12:49 AM
  • Here is something that might be of interest, run this code and see if it runs okay. Just change the name from DataGridView1 to the name of your DataGridView.

    Dim arrayOfSelectedRow = (DataGridView1.SelectedRows.Cast(Of DataGridViewRow)().
            Select(Function(row, index) New With {Key .Row = row, row.Cells(0).Value}).
            OrderBy(Function(item) item.Row.Index)).ToArray()
    
    For Each item In arrayOfSelectedRow
        If Not item.Row.IsNewRow Then
            Console.WriteLine(String.Join(",",
                item.Row.Cells.Cast(Of DataGridViewCell).
                    Select(Function(c) c.Value.ToString())))
        End If
    Next


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, March 2, 2019 1:16 AM
    Moderator
  • Hi, 

    I Fix The Error By This Way.

     Private Sub Dgw1_MouseClick(sender As Object, e As MouseEventArgs) Handles Dgw1.MouseClick
    
     Try
               If Dgw1.SelectedRows.Count = 0 Then
                    Exit Sub
                  If Dgw1.Rows.Count > 0 AndAlso Dgw1.CurrentRow IsNot Nothing Then
                     Dim dr As DataGridViewRow = Dgw1.SelectedRows(0)  
                     txtID.Text = dr.Cells(0).Value.ToString()
                     txtCoupleCode.Text = dr.Cells(1).Value.ToString()
    
                    End If
                End If
    End Sub

    Thank you Very Much.

    Saturday, March 2, 2019 9:11 AM