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

How to get selected row value

Hello everyone,


I want to get selected row value of my sfDataGrid but i have an exception

this is my source code :

 var selectedItem = grid.SelectedItems[0] ;

 DataRowView dataRowView = (DataRowView)selectedItem;

 var dataRow = dataRowView.Row;

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


I have this exception in the Second line (the cast)


System.InvalidCastException: 'Unable to cast object of type 'System.Data.Entity.DynamicProxies.Tache_99E51ACF7DD24743735103D1BFA461995A0264D449C0B92EEC2511C31C411E25' to type 'System.Data.DataRowView'.'

Can you help me and thank you 



 


4 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team November 17, 2022 02:19 PM UTC

The reported problem occurs due to mismatched typecast of getting the SelectedItems in SfDataGrid.

SelectedItems get by two cases based on DataSource bound type in SfDataGrid. Please refer to the below cases,

Case 1:

If you are using DataTable as DataSource for the SfDataGrid, you should cast the SelectedItems to DataRowView as shown below,

private void OnGetRowDetailsofDataTableClicked(object sender, EventArgs e)

{

    //DataSource as DataTable

    var selectedItem = sfDataGrid1.SelectedItems[0];

 

    //Here typecast based on DataRowView to get data

    DataRowView dataRowView = (DataRowView)selectedItem;

 

    var dataRow = dataRowView.Row;

 

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

  

}


Case 2:

A collection of OrderInfo type objects (List<OrderInfo> or ObservableCollection<OrderInfo>) will be assigned as DataSource for the SfDataGrid, you should cast the SelectedItems to underlying data object or underline business object or underline model as shown below,

  private void OnGetRowDetailsofCollectionClicked(object sender, EventArgs e)

  {

      //DataSource as Collection Type           

      var selectedItem = sfDataGrid1.SelectedItems[0];

      //Here typecast based on underlying business object to get data

      OrderInfo dataRowView = (OrderInfo)selectedItem;

 

      //Here get the cell value

      var cellValue = dataRowView.CustomerName;          

  }


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

https://help.syncfusion.com/windowsforms/datagrid/selection#getting-the-cell-value-by-using-cell-click-event

Find the sample in the attachment.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDataGridDemo_b26706dc.zip

Marked as answer

OE OuSsaMa Ess November 17, 2022 07:01 PM UTC

Hello it works perfectly thank you  @Vijayarasan Sivanandham  for your help



VS Vijayarasan Sivanandham Syncfusion Team November 18, 2022 06:21 AM UTC

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.



OE OuSsaMa Ess November 18, 2022 01:09 PM UTC

Ok thank you , you are gentleman


Loader.
Live Chat Icon For mobile
Up arrow icon