cell value

if in standar datagridview we use current row.cell("").value  to get value
so how in sfdatagrid we use that syntax

9 Replies

FP Farjana Parveen Ayubb Syncfusion Team April 12, 2018 12:06 PM UTC

Hi Ramdhan, 
 
Thanks for contacting Syncfusion support. 

You can get the current cell value of SfDataGrid by using GetControlValue method in CurrentCell, please refer the below code example. 

Code Example: 

// Get the CurrentCellValue 
var currentCellValue = sfDataGrid.CurrentCell.CellRenderer.GetControlValue(); 

And you can get the cell value of particular row and column by using View.Records in SfDataGrid, please refer the below code example. 

Code Example: 
// Get the cell value for RowIndex = 5 and ColumnIndex = 3 
int rowIndex = 5; 
int columnIndex = sfDataGrid.TableControl.ResolveToGridVisibleColumnIndex(3); 
if (columnIndex < 0) 
    return; 
var mappingName = sfDataGrid.Columns[columnIndex].MappingName; 
var recordIndex = sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex); 
if (recordIndex < 0) 
    return; 
if (sfDataGrid.View.TopLevelGroup != null) 
{ 
    var record = sfDataGrid.View.TopLevelGroup.DisplayElements[recordIndex]; 
    if (!record.IsRecords) 
        return; 
    var data = (record as RecordEntry).Data; 
    var cellVaue = (data.GetType().GetProperty(mappingName).GetValue(data, null).ToString()); 
} 
else 
{ 
    var record1 = sfDataGrid.View.Records.GetItemAt(recordIndex); 
    var cellVaue = (record1.GetType().GetProperty(mappingName).GetValue(record1, null).ToString()); 
} 
 

  
Regards, 
Farjana Parveen A 



RA ramdhan April 13, 2018 02:16 AM UTC

i got an error, maybe i convert to vb.net . here's my code :

 Dim rowindex As Integer = 3
        Dim columnIndex As Integer = SfDataGrid1.TableControl.ResolveToGridVisibleColumnIndex(3)
        If (columnIndex < 0) Then Return
        Dim mappingName = SfDataGrid1.Columns(columnIndex).MappingName
        Dim recordIndex = SfDataGrid1.TableControl.ResolveToRecordIndex(rowindex)
        If (recordIndex < 0) Then Return

        If IsDBNull(SfDataGrid1.View.TopLevelGroup) = True Then
            Dim record = SfDataGrid1.View.TopLevelGroup.DisplayElements(recordIndex)
            If record.IsRecords Then
                Return
            Else
                Dim data = (TryCast(record, RecordEntry)).Data
                cellValue = (data.GetType().GetProperty(mappingName).GetValue(data).ToString())
            End If
        Else
            Dim record1 = SfDataGrid1.View.Records.GetItemAt(recordIndex)
            cellValue = (record1.GetType().GetProperty(mappingName).GetValue(record1).ToString())
        End If


FP Farjana Parveen Ayubb Syncfusion Team April 13, 2018 10:31 AM UTC

Hi Ramdhan, 

Thanks for your response. 

We have converted the provided C# sample into VB.Net and it works fine on our end, please refer the below VB.Net sample. On analyzing your provided code example, we have found that the CellValue variable is not declared, this may cause the build error. If it is not your error, please share your error log that will help us to analyze further.  

  
Regards, 
Farjana Parveen A 



RA ramdhan April 16, 2018 03:24 AM UTC

here's the screenshot


AR Amal Raj U Syncfusion Team April 16, 2018 09:45 AM UTC

Hi Ramdhan, 

Thanks for your update. 

The reported exception from the given screenshot may be occurred due to GetProperty or GetValue becomes null, but the reported exception didn’t reproduce in our side. So, could you please provide us the below information. 

  • Whether the mentioned cell index is empty in underlying Data.
  • Whether you have used different data source? If data source is different, then please update us your data source.

In case of your data source is different and the mentioned cell index has empty data, value retrieved will be null, so that Null reference exception may occur when trying to ToString(). So, we have modified the given solution with exception handling with null check to retrieve the cell value. Please refer to the below updated sample and let me know, if the exception still reproduces or update us furthermore information. 

Modified Code example:   
'Get the cell value for RowIndex = 5 and ColumnIndex = 3   
Dim rowIndex As Integer = 5   
Dim columnIndex As Integer = sfDataGrid.TableControl.ResolveToGridVisibleColumnIndex(3)   
If columnIndex < 0 Then   
    Return   
End If   
Dim mappingName = sfDataGrid.Columns(columnIndex).MappingName   
Dim recordIndex = sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex)   
If recordIndex < 0 Then   
    Return   
End If   
If sfDataGrid.View.TopLevelGroup IsNot Nothing Then   
    Dim record = sfDataGrid.View.TopLevelGroup.DisplayElements(recordIndex)   
    If Not record.IsRecords Then   
        Return   
    End If   
    Dim data = (TryCast(record, RecordEntry)).Data   
    Dim cellVaue = (data.GetType().GetProperty(mappingName).GetValue(data, Nothing).ToString())   
Else   
    Dim record1 = sfDataGrid.View.Records.GetItemAt(recordIndex)   
    Dim cellValue = record1.GetType().GetProperty(mappingName).GetValue(record1, Nothing)   
    Dim cellString = String.Empty   
    If cellValue IsNot Nothing Then   
        cellString = cellValue.ToString()   
    End If   
    MessageBox.Show(cellString)   
End If   
  
Sample Link: 

Regards, 
Amal Raj U. 



TP Troy Pascoe June 12, 2018 02:06 AM UTC

Hi,

To read all you could try (dg is your sfdatagrid):

  foreach (RecordEntry record in dg.View.Records)
                {

                    var data = record.Data;

                   ExpandoObject eo = (ExpandoObject)data;
 
                    var dictionary = new Dictionary<string, object>(eo);

                    foreach (KeyValuePair<string, object> dic in dictionary)
                    {
                        var colName = dic.Key;
                        var val = dic.Value.ToString();
                 
                    }

                }


MA Mohanram Anbukkarasu Syncfusion Team June 12, 2018 12:23 PM UTC

Hi Troy, 
 
Thanks for your update. 
 
The cell values can also be retrieved as per your suggestion.  
  
Regards, 
Mohanram A. 



RA ramdhan August 20, 2018 06:04 AM UTC


Still Got Error


AA Arulraj A Syncfusion Team August 21, 2018 12:30 PM UTC

Hi Ramdhan, 

Thanks for your update. 

We suspect that you have used DataTable as the DataSource for the SfDataGrid. This may be the cause for the reported exception. Here I have provided the sample and code example for getting the cell value when the DataSource is a DataTable.  
Please refer to the below updated sample and let me know, if the exception still reproduces or update us furthermore information. 

Code Example: 

'Get the cell value for RowIndex = 5 and ColumnIndex = 3 
Dim rowIndex As Integer = 5 
Dim columnIndex As Integer = sfDataGrid.TableControl.ResolveToGridVisibleColumnIndex(3) 
If columnIndex < 0 Then 
    Return 
End If 
Dim recordIndex = sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex) 
If recordIndex < 0 Then 
    Return 
End If 
Dim record1 = sfDataGrid.View.Records.GetItemAt(recordIndex) 
If record1 IsNot Nothing Then 
    Dim cellValue = (TryCast(record1, DataRowView).Row.ItemArray(columnIndex)) 
    Dim cellString = String.Empty 
    If cellValue IsNot Nothing Then 
        cellString = cellValue.ToString() 
    End If 
    MessageBox.Show(cellString) 
End If 

 
Arulraj A 


Loader.
Up arrow icon