Click here to Skip to main content
14,693,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need some help retrieving cell data from a selected row of a wpf datagrid and I have no idea on how to achieve this. Please, can someone help me figure this out? Possibly post an example as well if you know how to get the data.

Thanks in advance!!
Posted
Updated 23-Aug-20 2:09am
v2

I think this answer by Bigsby[^] might help you.
   
Comments
Member 13116441 8-Jul-18 11:12am
   
This worked like a charm! thanks.

var person = grid.SelectedItem as Person;
if (person != null) textBlock.Text = person.FirstName;
These code works if you put the SelectionUnit property in FullRow.


private void GetSelectedRowCellValue()
{
    foreach (DataGridRow row in dataGrid1.SelectedItems)
    {
         System.Data.DataRow MyRow = (System.Data.DataRow)row.Item;
         string value = MyRow["columnname"].ToString();
    }
}
   
This thread goes way back, but if someone new stumbles upon this situation this might help:

In addition to what 'adocas' said, which was not working for me because of conversion between DataGridRow & DataRow not working, I came up this solution:


private void yourDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    foreach (DataRowView row in yourDataGrid.SelectedItems)
    {
        System.Data.DataRow MyRow = row.Row;
        string value = MyRow["columnname" (or index)].ToString();
        //MessageBox.Show(value); <- To check if it works...
    }
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900