Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read value from a Cell from a WPF DataGrid?

Tags:

c#

wpf

datagrid

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?

like image 367
KMC Avatar asked Apr 05 '11 08:04

KMC


3 Answers

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

like image 101
biju Avatar answered Sep 27 '22 23:09

biju


This might help someone else.

foreach (DataRowView row in dgLista.SelectedItems)
{
    string text = row.Row.ItemArray[index].ToString();
}

Good luck!

like image 38
BlackCath Avatar answered Sep 27 '22 21:09

BlackCath


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();
}
like image 24
icernos Avatar answered Sep 27 '22 22:09

icernos