locked
how to solve this error RRS feed

  • Question

  • Hi members

    when i reuse the datagridview i have this error wich probably from null reference

    Dim var As DataGridViewColumn = DataGridView1.Columns(0)
            var.Width = 80


    and the error is : 

    System.NullReferenceException : 'La référence d'objet n'est pas définie à une instance d'un objet.'

    please help

    Wednesday, February 5, 2020 8:41 AM

All replies

  • It means that the column has been disposed which is why you are getting the error.

    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

    Wednesday, February 5, 2020 10:35 AM
  • and how i solve it
    Wednesday, February 5, 2020 2:11 PM
  • and how i solve it

    You need to create it

    If DataGridView1.Columns.Count > 0 Then
        Dim var As DataGridViewColumn = DataGridView1.Columns(0)
        var.Width = 80
    Else
        '
        ' no columns exists, create it
        '
    End If


    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

    Wednesday, February 5, 2020 2:26 PM
  • but in 1st entry it goes normal

    this hapen in second entry

    i just closed the form and opened

    Wednesday, February 5, 2020 3:07 PM
  • Hi,

    There may now be empty cells that will cause System.NullReferenceException, and you need to check if the value is nothing before you reuse it, like the reply above.

    Then use the following code.

    DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            DataGridView1.AllowUserToResizeColumns = True
            Dim var As DataGridViewColumn = DataGridView1.Columns(0)
            var.Width = 80

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, February 6, 2020 2:39 AM
  • thanks for help but

    how i can check if there is a cell have a null value in datagridview or datatable ?

    so i will replace it with a space for example


    Thursday, February 6, 2020 8:00 AM
  • it work for me thanks a lot

    but it also appear in other line

                 
    e.Graphics.DrawString(DataGridView1.Rows(r).HeaderCell.Value.ToString, DataGridView1.Font, Brushes.Black, cell, sf)   

    same error here and i tried

    DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewAutoSizeColumnMode.None
      DataGridView1.AllowUserToResizeColumns = True

         

    not work please help

    Thursday, February 6, 2020 8:38 AM