We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Get cell value o a datagrid cell

I have a sfdatagrid and have the following code to get a cell value. Unfortunately, this code returns an empty string if the row hasn't yet been saved to the datasource (an observable collection). I trap for a right mouse click and then want to get the value of the clicked cell and open a dialog based on the cell value. How can I get the value of a cell if the data hasn't been saved to the datasource?

    Private Function GetCellValue(ByVal RowNum As Integer, ByVal ColumnNum As Integer) As String
        Dim CellString As String = String.Empty
        Dim MappingName As String
        Dim RecordIndex As Integer
        Dim Record As NodeEntry
        Dim RecordOb As Object
        Dim Data As Object
        Dim CellValue As Object
        If ColumnNum < 0 Then
            Return String.Empty
        End If
        MappingName = gridReceipts.Columns(ColumnNum).MappingName
        RecordIndex = gridReceipts.TableControl.ResolveToRecordIndex(RowNum)
        If recordIndex < 0 Then
            Return String.Empty
        End If
        If gridReceipts.View.TopLevelGroup IsNot Nothing Then
            Record = gridReceipts.View.TopLevelGroup.DisplayElements(RecordIndex)
            If Not record.IsRecords Then
                Return String.Empty
            End If
            Data = (TryCast(Record, RecordEntry)).Data
            CellString = (Data.GetType().GetProperty(MappingName).GetValue(Data, Nothing).ToString())
            RecordOb = gridReceipts.View.Records.GetItemAt(RecordIndex)
            CellValue = RecordOb.GetType().GetProperty(MappingName).GetValue(RecordOb, Nothing)
            If CellValue IsNot Nothing Then
                CellString = CellValue.ToString()
            End If
        End If
        Return cellstring
    End Function

5 Replies

TK Tom Krieg January 4, 2020 08:18 AM UTC

I've decided a different approach may be needed so I propose to get the "Newvalue" of the cell in the CurrentCellValidating event and store it someplace in a variable. I need the new value of an Order Number cell so if the user right clicks on a LineItem cell a dialog form is displayed from which the user picks the line item and that value is filled in in the appropriate cell in the grid. I have similar functionality for a number of cells in a row and I need the value of cells before the row is completed and written to the underlying datasource. My questions are

Is this a reasonable approach?
Is it possible to define a number of variables for each row in the grid?

SA Saravanan Ayyanar Syncfusion Team January 6, 2020 02:39 PM UTC

Hi Tom, 
Thank you for using Syncfusion control. 
CurrentCellValidating event is correct approach to get the cell value before commit the cell value. 
Do you want to show the row values as context menu, when right click the cell value and commit that selected value to grid cell? 
Can you please confirm this, it will helpful for us to investigate further. 
Saravanan A. 

TK Tom Krieg January 6, 2020 11:56 PM UTC

Hello Saravanan,

Basically yes. The cell where right click is to be trapped is a part number. On right click, I open a dialog form, which is basically a catalog where the user can search for the correct part. Once the user selects the correct part, that becomes a property and I want to get that property and fill the cell with that value. I also have other cells with similar behaviour.

TK Tom Krieg January 7, 2020 02:59 AM UTC

I also have another cell, a checkbox, where the validation of the whole row depends if the checkbox is checked or not. So validation rules for the row change depending on whether the checkbox is checked or not. I note that the value of a checkbox cell cannot be determined in the CurrentCellValidating event. So I'm still trying to work that out with no success. My next question is how to determine the value of a checkbox cell in the validating event. For example, the checkbox must not be checked if another cell in the same row has a specific value.

SA Saravanan Ayyanar Syncfusion Team January 7, 2020 12:06 PM UTC

Hi Tom,  
Thanks for your update.  
To show the row value as context menu, when rightclick the cell value and commit that selected value to grid cell  
We have prepared the sample as per your requirement by using CellClick event in SfDataGrid. Please find the sample link below the table. 
Validation for the checkbox column  
By default SfDataGrid doesn’t support the RowValidating event for non editable columns. You can achieve your requirement to show the error icon in row header for Boolean column by using the IDataErrorInfo interface.  
Please find the below code snippet. 
public class OrderInfo : INotifyPropertyChanged, IDataErrorInfo   
public string this[string columnName]   
        return string.Empty;   
[Display(AutoGenerateField = false)]   
public string Error   
        if (this.IsClosed == false)   
            return "Stock is not available";   
        return string.Empty;   
Please find the below UG link. 
Please refer the UG in below link for limitation in validation,   
Sample Link:  
Please let us know, if you require further assistance on this.  
Saravanan A.  

Live Chat Icon For mobile
Up arrow icon