Search code examples
c#wpfdatagriddatarowview

Return Cell 0 as a string from the selected Row in a DataGrid


I want to return the value of cell 0 from a row I double clicked on in a DataGrid. So far I can return the values of cell 0 from all rows, but I only want the cell 0 value of the row I double clicked on.

This is similar to this question which I iterate through in my example code.

private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    foreach (DataRowView row in dataGrid.Items)
    {
        string text = row.Row.ItemArray[0].ToString();
        Debug.WriteLine(text);
    }
}

Solution

  • Use SelectedItems instead of Items

    foreach (DataRowView row in dataGrid.SelectedItems)
    {
        string text = row.Row.ItemArray[0].ToString();
        Debug.WriteLine(text);
    }