Title: WPF MouseDown on Datagrid => Get Column and Row
Tags: wpf;events;datagrid
Question: how can I recognize where I click on ```DataGrid```?

```<DataGrid x:Name="TheGrid" SelectionMode="Single" SelectionUnit="Cell" MouseDown="CellClick"&gt;

private void CellClick(object sender, MouseButtonEventArgs e)
{
    foreach (DataGridCellInfo cell in TheGrid.SelectedCells)
    {
        MessageBox.Show(TheGrid.Items.IndexOf(cell.Item).ToString());
    }
}
```

many thanks

Comment: Sorry, I dont understand how?

Comment: I edit my Question. But this only work on >MouseUP< on MouseDown I get the "last" Cell... That's not the clean solution I'd like. is it possible to have a trigger that passes the line?

Here is the accepted answer: You could handle the ```SelectedCellsChanged``` event like this:

```private void TheGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    if (TheGrid.SelectedCells.Count &gt; 0)
    {
        DataGridCellInfo dgci = TheGrid.SelectedCells[0];
        int columnIndex = dgci.Column.DisplayIndex;
        DataGridRow row = TheGrid.ItemContainerGenerator.ContainerFromItem(dgci.Item) as DataGridRow;
        int rowIndex = row.GetIndex();

        MessageBox.Show($"Row {rowIndex} Column {columnIndex}");

    }
}
```


Here is another answer: A cell will get selected on MouseUp. To get the cell before this event occurred, you will have to listen on MouseDown on the Datagrid and check which element is under the mouse with VisualTreeHelper.HitTest.
Checkout this answer.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:04/03/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<wpf><events><datagrid>', 'Title': 'WPF MouseDown on Datagrid => Get Column and Row', 'ViewCount': 2026, 'id': 49635878, 'user_AboutMe': '<p>BY DAY: Alt-Rock Ninja Cowgirl at Veridian Dynamics.</p>\n\n<p>BY NIGHT: I write code and code rights for penalcoders.example.org, an awesome non-profit that will totally take your money at that link. My kids are cuter than yours.</p>\n\n<p>FOR FUN: C+ Jokes, Segway Roller Derby, NYT Sat. Crosswords (in Sharpie!), Ostrich Grooming.</p>\n\n<p>"If you see scary things, look for the helpers-you\'ll always see people helping."-Fred Rogers</p>\n', 'user_AccountId': 13018977.0, 'user_DisplayName': 'beari7', 'user_Reputation': 105.0, 'user_Views': 49.0, 'user_id': 9409093.0}}