Click here to Skip to main content
Rate this: bad
good
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
 
Thanks
Posted 11-Jul-13 0:17am
ErBhati2.8K
Edited 11-Jul-13 1:22am
v2
Comments
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
good
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;
                    MessageBox.Show(cellContent.Text);
               }
           }
         }
 

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;
        }
     }
  
</datagridrow>
  Permalink  
Comments
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
   
Thanks
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.
Thanks.

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 11 Jul 2013
Copyright © CodeProject, 1999-2014
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