How to get cellValue on a row seleted?

I have a small sfDatagrid with several columns and the first column is an ID field. How do I get this ID From a right click context menu? I have tried many scenarios, but I want to be able to click anywhere on the subject row, and get that rows ID. A good example of this would be a right click delete command.


Thanks in advance.


1 Reply

VS Vijayarasan Sivanandham Syncfusion Team December 6, 2021 07:33 AM UTC

Hi Donald,

Thank you for contacting Syncfusion Support.

Your requirement to get cellValue on a row selected in SfDataGrid can be achieved by using View.GetPropertyAccessProvider.GetValue method in SfDatGrid. Please refer the below code snippet,

 
//ContextMenu for SfDataGrid 
this.sfDataGrid1.RecordContextMenu = new ContextMenuStrip(); 
this.sfDataGrid1.RecordContextMenu.Items.Add("Delete", null, OnDeleteClicked); 
private void OnDeleteClicked(object sender, EventArgs e) 
{             
           if (sfDataGrid1.SelectedItem != null) 
           { 
                //get the selected row data 
                var data = sfDataGrid1.SelectedItem; 
                 
                if (this.sfDataGrid1.View != null) 
                { 
                    //get the cell value ID Column 
                    var cellValue = this.sfDataGrid1.View.GetPropertyAccessProvider().GetValue(data, "ID"); 
 
                    MessageBox.Show("Selected row cell value of ID :" + cellValue); 
                } 
            } 
} 

Sample Link: https://www.syncfusion.com/downloads/support/forum/170918/ze/SfDataGridDemo122796585

For more information related to Get the Cell Value, please refer the below user guide documentation link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/selection#get-the--cell-value

https://help.syncfusion.com/windowsforms/datagrid/interactivefeatures#context-menu

Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 


Loader.
Up arrow icon