Click here to Skip to main content
11,709,033 members (61,984 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET
Hi Experts
I want to get values of each cell for every row in dataGrid in WPF On a button click event .Button is Outside of the DataGrid
plz help

Posted 10-Jul-13 23:17pm
Edited 11-Jul-13 0:22am
Richard MacCutchan at 11-Jul-13 5:44am
Assuming you mean a DataGridView object then you just need to add some code in your button click handler. If that is not the case then please edit your question and explain your problem in more detail.
ErBhati at 11-Jul-13 6:19am
Its Not a DataGridView Of Windows Form. Its a DataGrid Of WPF(Windows Presentation Foundation).

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use this in youer event handler

var rows = GetDataGridRows(datagrid1);
foreach (DataGridRow r in rows)
 //   DataRowView rv = (DataRowView)r.Item;
  foreach (DataGridColumn column in datagrid1.Columns)
      if (column.GetCellContent(r) is TextBlock)
           TextBlock cellContent = column.GetCellContent(r) as TextBlock;
Method to get the rows from datagrid
 public IEnumerable<datagridrow> GetDataGridRows(DataGrid grid)
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
          var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                    if (null != row) yield return row;
ErBhati at 11-Jul-13 7:01am
public IEnumerable GetDataGridRows(DataGrid grid)
var itemsSource = grid.ItemsSource as IEnumerable;

i have some errors in as IEnumerable; and public IEnumerable line
Naz_Firdouse at 11-Jul-13 7:06am
include namespaces
using System.Collections;
using System.Collections.Generic;
ErBhati at 11-Jul-13 7:39am
ErBhati at 11-Jul-13 7:44am
Little more help sir ,How i Bind cellContent value to the datatable every time
ErBhati at 12-Jul-13 2:58am
Thanks sir It give me good Idea..Thanks alot
Naz_Firdouse at 12-Jul-13 3:04am
Please mark it as answer and vote up so that it will be helpful to others
vishal jodh at 17-Jun-14 10:41am
When i am trying to use GetDataGridRows() method it does not get all row info.
only get 7 rows, i think this is because my UI showing 7 row view at a time.
any solution for such issue.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 580
1 Maciej Los 235
2 Peter Leow 219
3 OriginalGriff 214
4 Mika Wendelius 170
0 OriginalGriff 9,348
1 Sergey Alexandrovich Kryukov 8,727
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 11 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100