Sam
Sam

Reputation: 45

Finding a cell in devexpress datagrid

I'm new to DevExpress GridControl. I need to find a particular cell in the grid and then do something with it's value. How do I go about this please?

In the grid's Loaded method, I tried using myGrid.FindRowByValue("ProductType","ABC"), but always gives a negative number.

Thanks.

Upvotes: 1

Views: 1522

Answers (2)

Niranjan Singh
Niranjan Singh

Reputation: 18290

Unlike XtraGrid, the DXGrid for WPF does not provide the DataRowCount property - that is why we suggested checking the GridControl's ItemsSource. On the other hand, our grid has the VisibleRowCount property, which will be useful in some scenarios.

To accomplish this task, iterate through visible grid rows manually as shown below.

void MoveFocusToLast(GridControl grid, string fieldName, object value) {
    for (int i = grid.VisibleRowCount - 1; i >= 0; i--) {
        var handle = grid.GetRowHandleByVisibleIndex(i);
        var currentValue = grid.GetCellValue(handle, fieldName);
        if (currentValue != null && currentValue.Equals(value)) {
            grid.View.FocusedRowHandle = handle;
            return;
        }
    }
}

Grid also provides the FindRowByValue method, which allows you to find a row by a specific cell value. This method returns the handle of the corresponding row, and you can make that row visible by setting the FocusedRowHandle property or calling ScrollIntoView. I have prepared a sample demonstrating this approach.

See Also:
Traversing Rows
Find Row
Get RowHandle from a cell value

Upvotes: 0

user9170420
user9170420

Reputation:

Here is code you can try

for (int i = 0; i < gridView1.DataRowCount; i++) {
    object b = gridView1.GetRowCellValue(i, "FieldName");
    if (b != null && b.Equals(<someValue>)){
        gridView1.FocusedRowHandle = i;
        return;
    }
}

you can go to this link for more details.

https://www.devexpress.com/Support/Center/Question/Details/Q132599/get-row-by-cell-value

Upvotes: 1

Related Questions