How to read value from a Cell from a WPF DataGrid?
Asked Answered
M

6

6

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?

Metaplasm answered 5/4, 2011 at 8:44 Comment(0)
V
3

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

Vernice answered 5/4, 2011 at 8:47 Comment(5)
from your answer it seems I have to write a helper function to get one cell value out of a DataGrid. I have hundreds of rows and multiple columns in my DataGrid. To get all values I have to loop through all rows and run the helper functions hundreds of time. Is there a quicker way?Metaplasm
@Metaplasm It depends on what you are trying to do.Why do you want to loop through all the cells and get the data in cells ? You can simply use databinding to your code behind or viewmodel and loop your collection instead.Vernice
I have to select a good number of cells in a DataGrid and output to an Excel sheet. That's why I'm looking for a more intuitive way to do that. I will reword my question and post a new question.Metaplasm
@Vernice - Please edit this answer so it can be understood without following the links so we are safe from link-rot.Vague
@Metaplasm No helpers should be needed. Learn XAML, use data binding. Doing this stuff in c# with XAML is always a mess -- you're fighting against the framework. He's helping you do it the wrong way, but it's still the wrong way.Daw
W
4

This might help someone else.

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

Good luck!

Wasp answered 12/2, 2013 at 1:29 Comment(0)
B
4

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();
}
Baden answered 13/4, 2016 at 0:8 Comment(0)
V
3

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

Vernice answered 5/4, 2011 at 8:47 Comment(5)
from your answer it seems I have to write a helper function to get one cell value out of a DataGrid. I have hundreds of rows and multiple columns in my DataGrid. To get all values I have to loop through all rows and run the helper functions hundreds of time. Is there a quicker way?Metaplasm
@Metaplasm It depends on what you are trying to do.Why do you want to loop through all the cells and get the data in cells ? You can simply use databinding to your code behind or viewmodel and loop your collection instead.Vernice
I have to select a good number of cells in a DataGrid and output to an Excel sheet. That's why I'm looking for a more intuitive way to do that. I will reword my question and post a new question.Metaplasm
@Vernice - Please edit this answer so it can be understood without following the links so we are safe from link-rot.Vague
@Metaplasm No helpers should be needed. Learn XAML, use data binding. Doing this stuff in c# with XAML is always a mess -- you're fighting against the framework. He's helping you do it the wrong way, but it's still the wrong way.Daw
G
1

for wpf use:

DataRowView item = dataGrid.Items[rowIndex] as DataRowView;
Console.WriteLine(item.Row.ItemArray[columIndex]);
Genotype answered 29/3, 2021 at 10:52 Comment(0)
C
0

The following helped me:

Private Sub dgNames_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles dgNames.MouseDoubleClick

    Dim strCellContent as String = MessageBox.Show(TryCast(e.OriginalSource, TextBlock).Text)

End Sub
Cockayne answered 11/10, 2015 at 19:2 Comment(0)
L
0
foreach (DataRowView row in pokemonGrid.SelectedItems)
{
    string text = row["columnName"].ToString();
    Trace.WriteLine(text);
}

This worked for me! Thanks to @BlackCath for pointing me in the right direction :)

Logistic answered 29/7, 2024 at 21:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.