How to make loop on selected rows in SfDataGrid and get values of the cells in each row

hi
I'm trying to get values of cells from the selected rows so how to make loop on selected rows in SfDataGrid and get values of the cells in each row?

1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team February 2, 2021 09:33 AM UTC

Hi Mohamad,

Thank you for contacting Syncfusion support.

Your requirement can be achieved by using SelectedItems collection in SfDataGrid, by converting SelectedItem into underlying business object. Please refer the below code snippet for your reference, 
private void BtnGetValue_Click(object sender, EventArgs e) 
{ 
            txtDisplayRecord.Text = "OrderID\tCustomerID\tCustomerName\tCountry\tShipCity\tIsShipped\t"; 
 
            //Get the SelectedItems Collection in SfDataGrid.SelectedItems  
            foreach (var selectedItem in this.sfDataGrid.SelectedItems) 
            { 
                //get the Observable collection selected row 
                //get the selected row value in SelectedItems collection in SfDataGrid 
                //by converting into underlying business object. 
                var dataRow = selectedItem as OrderInfo; 
                txtDisplayRecord.Text += dataRow.OrderID + "\t" + dataRow.CustomerID + "\t\t" + dataRow.CustomerName + "\t" + dataRow.Country + "\t" + dataRow.ShipCity + "\t" + dataRow.IsShipped + "\t"; 
 
 
                ////get the DataTable collection selected row  
                //var dataRow1 = (selectedItem as DataRowView).Row; 
                ////mention the column mapping name or column index to get cell value in Selected row     
                //txtDisplayRecord.Text += dataRow1["OrderID"].ToString() + "\t" + dataRow1["CustomerID"].ToString() + "\t\t" + dataRow1["CustomerName"].ToString() + "\t" + dataRow1["Country"].ToString() + "\t" + dataRow1["ShipCity"].ToString() + "\t" + dataRow1["IsShipped"].ToString() + "\t"; 
                ////txtDisplayRecord.Text += dataRow1[0].ToString() + "\t" + dataRow1[1].ToString() + "\t\t" + dataRow1[2].ToString() + "\t" + dataRow1[3].ToString() + "\t" + dataRow1[4].ToString() + "\t" + dataRow1[5].ToString() + "\t";  
            } 
} 
https://help.syncfusion.com/windowsforms/datagrid/selection?cs-save-lang=1&cs-lang=csharp#retrieving-the-current-item-and-display-in-message-box

We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon