Error getting value of a cell from datagrid

Hi

I'm trying to retrieve the value of a cell from a row in SFDatagrid. I'm using VB.Net and the code example provide at   Selection | DataGrid Control for Windows Forms | Syncfusion

My code is therefore as follows:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim cellValue As String
    Dim rowIndex As Integer = 5
    Dim columnIndex As Integer = Me.dgvPersonTable.TableControl.ResolveToGridVisibleColumnIndex(2)
    If columnIndex < 0 Then
        Return
    End If
    Dim mappingName = dgvPersonTable.Columns(columnIndex).MappingName
    Dim recordIndex = dgvPersonTable.TableControl.ResolveToRecordIndex(rowIndex)
    If recordIndex < 0 Then
        Return
    End If
    If dgvPersonTable.View.TopLevelGroup IsNot Nothing Then
        Dim record = dgvPersonTable.View.TopLevelGroup.DisplayElements(recordIndex)
        If Not record.IsRecords Then
            Return
        End If
        Dim data = (TryCast(record, RecordEntry)).Data
        cellValue = (data.GetType().GetProperty(mappingName).GetValue(data, Nothing).ToString())
    Else
        Dim record1 = dgvPersonTable.View.Records.GetItemAt(recordIndex)
        cellValue = (record1.GetType().GetProperty(mappingName).GetValue(record1, Nothing).ToString())
    End If


    MessageBox.Show(cellValue, "Value in cell (" & rowIndex & ", " & columnIndex & ")")
End Sub

When executed this returns an unhandled exception as follows:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Type.GetProperty(...) returned Nothing.

The error is being thrown at the line "cell value = (record1.getType().......etc".Can anyone suggest where this code is going wrong please.

Thanks and regards


Michael Bond


3 Replies 1 reply marked as answer

DD Dhivyabharathi Dakshinamurthy Syncfusion Team June 6, 2024 04:20 PM UTC

Hi Michael Bond,

We analyzed your query based on the information provided you are encountering the exception "System.NullReferenceException: 'Object reference not set to an instance of an object.'" We suspect you are using a DataTable instead of an ObservableCollection.

If you are using a DataTable, kindly refer to the code snippets below.

 

cellValue = (record1 as DataRowView).Row[mappingName].ToString();


Output:

A screenshot of a computer

Description automatically generated


We hope this helps to achieve your requirement. Please let us know, if need any further assistance.



Attachment: CellValue_SfDataGrid_38eedac.zip

Marked as answer

MI Michael June 7, 2024 10:01 AM UTC

Many thanks, that solved the issue


Kind regards


Michael Bond



DD Dhivyabharathi Dakshinamurthy Syncfusion Team June 10, 2024 08:47 AM UTC

Hi Michael,

We hope that your issue has been resolved with our proposed solution, please get back to us if you need further assistance. We are happy to help you.


Loader.
Up arrow icon