Hi,
I am using sfDataGrid to load the first column in every row after ensuring that the row has been selected by determining the state of the checkbox column,I followed the answer from this forum post https://www.syncfusion.com/forums/139154/loop-through-sfdatagrid-rows
However whenever the program executes this line of code
var dataRowView = record.Data as DataRowView;
It results in the loss of data and the dataRowView object returns a NULL value
I have validated record.Data and there's no issue there however as soon as the casting takes place the object is set to NULL
I have to add that the sfDataGrid used here takes a list as it's DataSource and the checkbox column is unbounded
What could be the issue here?
Here is all the relevant code
private void ReturnCodes() { var records = sfDataGrid1.View.Records; foreach (var record in records) { var dataRowView = record.Data as DataRowView; if (validator.isEmpty(record.Data)) { Messenger.DebugMsg.CustomMessage("record.Data is empty"); } if (dataRowView != null) { Messenger.DebugMsg.CustomMessage("Selected"); //var selectedValue = dataRowView.Row["name"]; //var selected = dataRowView.Row["SelectorColumn"]; //if (selected.GetType() != typeof(DBNull) && (bool)selected) //{ // //To do // var Code = selectedValue as ItemListInfo; // POCodes.Add(Code.item_code); //} } else { Messenger.DebugMsg.CustomMessage("NULL GridView"); } } } |
private void GenerateColumns() { // Clear columns sfDataGrid1.Columns.Clear(); // PO Code SfDataGridHelper.AddGridTextColumn(sfDataGrid1, "name", "PO Code"); // Date SfDataGridHelper.AddDateTimeColumn(sfDataGrid1, "transaction_date", "Date"); // Branch SfDataGridHelper.AddGridTextColumn(sfDataGrid1, "company", "Branch"); // Total Value SfDataGridHelper.AddGridNumericColumn(sfDataGrid1, "grand_total", "Total Value"); // Mark For Delivery SfDataGridHelper.AddGridTextColumn(sfDataGrid1, "mark_for_delivery", "Mark For Delivery"); // Created By SfDataGridHelper.AddGridTextColumn(sfDataGrid1, "owner", "Created By"); // Posted By SfDataGridHelper.AddGridTextColumn(sfDataGrid1, "modified_by", "Posted By"); // Checkbox SfDataGridHelper.AddGridCheckBoxColumn(sfDataGrid1, "SelectorColumn", String.Empty); // Apply settings DataGridSettings(); } |
private void DataGridSettings() { // Dont Auto Generate columns sfDataGrid1.AutoGenerateColumns = false; // Allow filtering sfDataGrid1.AllowFiltering = true; // Allow sorting sfDataGrid1.AllowSorting = true; // Auto Size mode sfDataGrid1.AutoSizeColumnsMode = AutoSizeColumnsMode.Fill; // Selection Mode sfDataGrid1.SelectionMode = GridSelectionMode.Multiple; // Dont allow editing sfDataGrid1.AllowEditing = false; } |
I'm not sure what I am missing here but please point me in the right direction
Hi Ammar,
The reported problem occurs due to a mismatched typecast while retrieving DataRow
in SfDataGrid.
DataRow gets 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 RowData to DataRowView like below mentioned code snippet,
|
private void OnGetRowDetailsofDataTableClicked(object sender, EventArgs e) { //DataSource as DataTable foreach (var record in sfDataGrid1.View.Records) { //Here typecast based on DataRowView to get data var dataRowView = record.Data as DataRowView;
if (dataRowView != null) { //get the row data details var getOrderIdvalue = dataRowView.Row["OrderID"]; var getModuleCodevalue = dataRowView.Row["ModuleCode"]; var getCustomerNamevalue = dataRowView.Row["CustomerName"]; var getCountryvalue = dataRowView.Row["Country"]; var getShipCityvalue = dataRowView.Row["ShipCity"]; var getSelectorColumnvalue = dataRowView.Row["SelectorColumn"]; } } } |
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 RowData to the underlying data object or underline
business object or underline model like below mentioned code
snippet,
|
private void OnGetRowDetailsofCollectionClicked(object sender, EventArgs e) { //DataSource as Collection Type foreach (var record in sfDataGrid1.View.Records) { //Here typecast based on underlying business object to get data var dataRowView = record.Data as OrderInfo;
if (dataRowView != null) { //get the details of record var getOrderIdvalue = dataRowView.OrderID; var getModuleCodevalue = dataRowView.ModuleCode; var getCustomerNamevalue = dataRowView.CustomerName; var getCountryvalue = dataRowView.Country; var getShipCityvalue = dataRowView.ShipCity; var getSelectorColumnvalue = dataRowView.SelectorColumn;
} } } |
Please find the sample in the attachment and let us know if you have any
concerns in this.
Regards,
Vijayarasan S