How to read a cell value form a WPF DataGrid?
I search online and try any possible combination and nothing works:
Datagrid.Cells[..], DataGrid.Items.Cells[..], DataGrid.Rows.., DataGrid.Items.Row.. nothing works, I can't find it is MSDN or I don't understand it. I just need to read a value off a cell in DataGrid that simple.
How to do it?
Check this
http://social.msdn.microsoft.com/Forums/en/wpf/thread/74332b78-6bfd-4ac9-af85-dfd9bec87a29
http://wpfadventures.wordpress.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row/
WPF Toolkit DataGrid SelectionChanged Getting Cell Value
This might help someone else.
foreach (DataRowView row in dgLista.SelectedItems)
{
string text = row.Row.ItemArray[index].ToString();
}
Good luck!
Here's a summary of the solution.
Winform
Type: System.windows.Forms.DataGridView
// C#
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//"Column1" = column name in DataGridView
string text = row.Cells["Column1"].value.ToString();
}
WPF equivalent
Type: DataGrid
// C#
foreach (DataRowView row in dataGrid.Items)
{
string text = row.Row.ItemArray[index].ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With