[RESOLVED] Accessing Last Row of a DataGrid
I am trying to access a column on the last row of my data grid but I am getting the following error - System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Below I have included my vb.net code
Count = Me.DataGridView1.Rows.Count
MsgBox("70 " + Me.DataGridView1.Rows(Me.DataGridView1.Rows.Count - 1)
MsgBox("71 " + Me.DataGridView1.Rows(19).Cells.Item(4).Value.ToString) <---- This is the line generating the exception.
Thanks
Re: Accessing Last Row of a DataGrid
The error was due the particular cell has the null values which when try converting to the string data type its throws an exception.
So before getting the value try check for null string in the cell. either isnull or <>""
Re: Accessing Last Row of a DataGrid
you can check something like
Code:
drv[x].ToString().Trim() == System.DBNull.Value.ToString()