Click here to Skip to main content
11,931,104 members (63,114 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 11-Jul-13 0:17am
Edited 11-Jul-13 1:22am
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).

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 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.

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
Web03 | 2.8.151126.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