DevExpress GridControl查找指定单元格失败,求解决方法
FindRowByValue Returning Negative in DevExpress GridControl Hey there! Let's break down why your FindRowByValue call is returning a negative number (which equals GridControl.InvalidRowHandle, meaning no matching row was found) and fix it step by step.
Common Causes & Fixes
1. You're Calling the Method on the Wrong Control
First things first: FindRowByValue is a method of GridView, not GridControl itself. If you're directly calling myGrid.FindRowByValue(...), that's the issue. You need to access the grid's main view (assuming you're using a GridView) first:
GridView gridView = myGrid.MainView as GridView; if (gridView == null) return; // Make sure we have a valid GridView int rowHandle = gridView.FindRowByValue("ProductType", "ABC");
2. Data Isn't Fully Loaded Yet
The Loaded event fires when the grid control finishes rendering, but your data source might not have finished binding or loading (especially if using async data fetching). Instead, use the DataBindingComplete event – this triggers after data is fully bound to the grid:
private void myGrid_DataBindingComplete(object sender, DevExpress.XtraGrid.Views.Base.DataBindingCompleteEventArgs e) { GridView gridView = myGrid.MainView as GridView; if (gridView == null || gridView.DataRowCount == 0) return; int rowHandle = gridView.FindRowByValue("ProductType", "ABC"); // Perform your cell value operations here }
3. Field Name Mismatch (Case-Sensitive!)
Double-check that "ProductType" exactly matches the field name in your data source (not the column's display caption). DevExpress is case-sensitive here – so producttype or Producttype won't work if your data source property is ProductType.
4. Value Type Doesn't Match
If your ProductType field isn't a string (e.g., it's an enum, int, or custom type), passing a string "ABC" won't match. For example, if it's an enum:
// If ProductType is an enum like ProductTypes.ABC int rowHandle = gridView.FindRowByValue("ProductType", ProductTypes.ABC);
Always ensure the value you pass matches the exact type of the data source field.
5. Filtering/Hiding Rows
If your grid has active filters or some rows are hidden, FindRowByValue might not detect them by default. Use the overload that includes filtered-out rows:
// Third parameter = true to look in filtered/hidden rows int rowHandle = gridView.FindRowByValue("ProductType", "ABC", true);
6. The Record Doesn't Actually Exist
It sounds obvious, but take a minute to verify your data source actually contains a record where ProductType equals "ABC". Debug and inspect your data source directly to rule this out.
Example: Working Code
Putting it all together, here's a complete snippet to find and update the cell:
private void myGrid_DataBindingComplete(object sender, DevExpress.XtraGrid.Views.Base.DataBindingCompleteEventArgs e) { GridView gridView = myGrid.MainView as GridView; if (gridView == null || gridView.DataRowCount == 0) { Debug.WriteLine("No data or invalid view"); return; } int rowHandle = gridView.FindRowByValue("ProductType", "ABC", true); if (rowHandle != GridControl.InvalidRowHandle) { // Update the cell value gridView.SetRowCellValue(rowHandle, "ProductType", "Updated_ABC"); // Optional: Focus the found row gridView.FocusedRowHandle = rowHandle; } else { Debug.WriteLine("No matching row found. Check field name, value type, and data source."); } }
内容的提问来源于stack exchange,提问作者Sam

