How to get DevExpress GridControl cell clicked in wpf?

3.1k views Asked by At

I have a wpf application where I using DevExpress GridControl with a TableView inside. My problem is that I want to get the clicked cell. I found on other post that solution:

private void TableView_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        TableViewHitInfo hitInfo = tableView.CalcHitInfo(e.GetPosition(MainControl));
        if (hitInfo.InRowCell)
        {
            object value = gridControl.MainView.GetRowCellValue(hitInfo.RowHandle, hitInfo.Column);
            //...
        }
    }

But grid control doesn't have a property named MainView. What i do wrong? Or have you other solution for my problem?

2

There are 2 answers

0
fk2 On

MainView is the name of the View the GridControl uses in this example. If you have renamed or named your view otherwise you obviously need to also change the code to use that name:

object value = MyGridControl.MyGridView.GetRowCellValue(hitInfo.RowHandle, hitInfo.Column);

or easier since the view should already be in scope:

object value = MyGridView.GetRowCellValue(hitInfo.RowHandle, hitInfo.Column);
0
DmitryG On

The correct code-snippet for obtaining cell-value should looks like this:

<dxg:GridControl ItemsSource="{Binding ...">
    <dxg:GridControl.View>
        <dxg:TableView AllowEditing="False" 
                       MouseLeftButtonUp="TableView_MouseLeftButtonUp"/>
    </dxg:GridControl.View>
</dxg:GridControl>


void TableView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
    TableView tableView = sender as TableView;
    TableViewHitInfo hitInfo = tableView.CalcHitInfo(e.OriginalSource as DependencyObject);
    if (hitInfo.InRowCell) {
        object value = tableView.Grid.GetCellValue(hitInfo.RowHandle, hitInfo.Column);
        // do something
    }
}

Related help-article: Obtaining and Setting Cell Values