Click here to Skip to main content
15,867,594 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 1:09am
v2

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:


C#
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...
    }
}
 
Share this answer
 
v2
Comments
Member 11318775 13-May-22 21:31pm    
Thanks, this worked for me
These code works if you put the SelectionUnit property in FullRow.


C#
private void GetSelectedRowCellValue()
{
    foreach (DataGridRow row in dataGrid1.SelectedItems)
    {
         System.Data.DataRow MyRow = (System.Data.DataRow)row.Item;
         string value = MyRow["columnname"].ToString();
    }
}
 
Share this answer
 
I think this answer by Bigsby[^] might help you.
 
Share this answer
 
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;

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