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
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.
Hello it works perfectly thank you @Vijayarasan Sivanandham for your help
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😊.
Ok thank you , you are gentleman