Click here to Skip to main content
12,402,241 members (36,811 online)
Rate this:
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
Updated yesterday
Richard MacCutchan 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 11-Jul-13 6:19am
Its Not a DataGridView Of Windows Form. Its a DataGrid Of WPF(Windows Presentation Foundation).
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 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 11-Jul-13 7:06am
include namespaces
using System.Collections;
using System.Collections.Generic;
ErBhati 11-Jul-13 7:39am
ErBhati 11-Jul-13 7:44am
Little more help sir ,How i Bind cellContent value to the datatable every time
ErBhati 12-Jul-13 2:58am
Thanks sir It give me good Idea..Thanks alot
Naz_Firdouse 12-Jul-13 3:04am
Please mark it as answer and vote up so that it will be helpful to others
vishal jodh 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

It is a software bug so :

change column visibility to visible.
remove DataGridTemplateColumn.
it will work good.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 26 Jul 2016
Copyright © CodeProject, 1999-2016
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