How to get selected row value

Hi there,

Assuming I have sfDataGrid like below :

            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Code", typeof(String));
            dt.Columns.Add("Name", typeof(String));
            sfDataGrid.DataSource = dt;

When I click a row, how I get the selected row["ID"] ?

In DataGridView, this is working good like code below :
if (dataGridView1.SelectedRows.Count != 0)
{
    DataGridViewRow row = this.dataGridView1.SelectedRows[0];
    row.Cells["ColumnName"].Value
}

Thank you

3 Replies

FP Farjana Parveen Ayubb Syncfusion Team May 8, 2018 10:24 AM UTC


Hi Jun, 
 
Thank you for contacting Syncfusion support. 

You can get the selected row value when click on the row by using the SelectionChanged event in SfDataGrid. 

void sfDataGrid_SelectionChanged(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangedEventArgs e) 
{ 
    var selectedItem = this.sfDataGrid.CurrentItem as DataRowView; 
    var dataRow = (selectedItem as DataRowView).Row; 
    var cellValue = dataRow["ID"].ToString(); 
} 

You can also get the selected row value in SelectedItems collection in SfDataGrid, by converting SelectedItem into underlying business object.  

Code Example: 
var selectedItem = sfDataGrid.SelectedItems[0]; 
var dataRow = (selectedItem as DataRowView).Row; 
var cellValue = dataRow["ID"].ToString(); 

Please refer the below code example and sample in the following location, 
 
 
  
Regards, 
Farjana Parveen A 
 



MO mohammed replied to Farjana Parveen Ayubb February 7, 2024 02:51 AM UTC

get an error when grouping

{

Object reference not set to an instance of an object

}


can you Reso



SB Sweatha Bharathi Syncfusion Team February 7, 2024 01:51 PM UTC

Hi Jun Rikson ,



We have analysed your query, it seems that the issue you are encountering occurs during the SelectionChanged event, especially when performing grouping. This is likely due to the creation of records based on the grouping, causing the CurrentItem to not be maintained.


To address this issue, we recommend to set a null check based on the CurrentItem in the SelectionChanged event to handle such scenarios.

Additionally, in our previous update, we provided an option for obtaining selected items in a button click event.


We have also included a sample for your reference. Please review the provided sample and let us know if you have any further concerns on this.



Code Snippet:


void sfDataGrid_SelectionChanged(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangedEventArgs e)

{

   

     if(this.sfDataGrid.CurrentItem != null)

     {

         var selectedItem = this.sfDataGrid.CurrentItem as DataRowView;

         var dataRow = (selectedItem as DataRowView).Row;

         var cellValue = dataRow["ID"].ToString();

     }

              

 }


Attachment: SelectedValue_c305017.zip

Loader.
Up arrow icon