Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6740795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:38:56+00:00 2026-05-26T11:38:56+00:00

We have such a scenario that we have a page including a DataGrid, and

  • 0

We have such a scenario that we have a page including a DataGrid, and now we want to get all data from this DataGrid, but without accessing to the underlying item source of it, i.e., we want to access to the data directly from the DataGrid. It seems to be tricky but not impossible. I found many articles, like this: DataGridHelper, and this: Get WPF DataGrid row and cell, and many other ones. They are basically the same thing: to define the extension methods on DataGrid with help of another GetVisualChild function to find the target DataGridCell object. However, when I am using it, I can’t find the target cell. Specifically, Each row in the DataGrid corresponds to one item from a collection of the DataContext, let’s say, it is a collection of type “Employee”, and each column of the DataGrid corresponds one property of class Employee, e.g, the Name, Gender, Age. Now my problem is, the above-mentioned GetCell() function always finds a DataGridCell with one Employee object as its content (the property of Content in DataGridCell), and can’t go further into each property, no matter what column index I give it.
For example, in the GetCell function, there is one line:
Dim cell As DataGridCell = DirectCast(presenter.ItemContainerGenerator.ContainerFromIndex(column), DataGridCell),
where the presenter is a DataGridCellsPresenter I got which representing the row I choose, and as soon as I give the column index, naturally I am expecting it to return the control for selected property at position I specified. But it just doesn’t work as expected. Any help would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:38:56+00:00Added an answer on May 26, 2026 at 11:38 am

    The moment you use presenter.ItemContainerGenerator.ContainerFromIndex you fall into a limitation for it to work ONLY for non-virtualized items i.e. rows that are shown in the scroll view (plus some offset number of rows above and below the scroll view limits) of the datagrid.

    For you to access values of all cells you will have to execute column level bindings for each row.

    1. Access the DataGrid.Items collection. This is a view of items so any items hidden by filter criteria or custom paging etc will be excluded. If you dont want that then do DataGrid.ItemsSource.Cast<object>().ToList() call.

    2. Now access all columns of the datagrid i.e. DataGrid.Columns. Assuming that they are of any type but DataGridTemplateColumn, step 3 below will extract the cell level value. For template columns you will have to specify some property value that represents the entire template of the cell. I find DataGridTemplateColumn.SortMemberPath a good candidate for this.

    3. Extract the DataGridTextColumn.Binding, DataGridCheckBoxColumn.Binding, DataGridComboBoxColumn.SelectedValueBinding or DataGridComboBoxColumn.SelectedItemBinding. Then for each item from step 1, execute the binding to extract the value.

    Code

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string gridContent = string.Empty;
    
            foreach(var item in MyDataGrid.Items)
            {
                foreach (var column in MyDataGrid.Columns)
                {
                    var textCol = column as DataGridTextColumn;
                    var checkCol = column as DataGridCheckBoxColumn;
                    var comboCol = column as DataGridComboBoxColumn;
                    var templateCol = column as DataGridTemplateColumn;
    
                    if (textCol != null)
                    {
                        var propertyName = ((Binding)textCol.Binding).Path.Path;
                        var value
                                = item.GetType().GetProperty(
                                      propertyName).GetValue(
                                      item,
                                      new object[] {});
    
                        if (((Binding)textCol.Binding).Converter != null)
                        {
                            value
                                = ((Binding)checkCol.Binding).Converter.Convert(
                                    value,
                                    typeof(object),
                                    ((Binding)checkCol.Binding).ConverterParameter,
                                    ((Binding)checkCol.Binding).ConverterCulture);
                        }
                        gridContent = gridContent + "\t" + value.ToString();
                    }
                    if (checkCol != null)
                    {
                        var propertyName = ((Binding)checkCol.Binding).Path.Path;
                        object value
                            = item.GetType().GetProperty(
                                   propertyName).GetValue(
                                   item,
                                   new object[] { });
    
                        if (((Binding)checkCol.Binding).Converter != null)
                        {
                            value
                                = ((Binding)checkCol.Binding).Converter.Convert(
                                    value,
                                    typeof(object),
                                    ((Binding)checkCol.Binding).ConverterParameter,
                                    ((Binding)checkCol.Binding).ConverterCulture);
                        }
                        gridContent = gridContent + "\t" + value.ToString();
                    }
                    if (comboCol != null)
                    {
                        var propertyName = string.Empty;
                        if (comboCol.SelectedValueBinding != null)
                        {
                            propertyName
                              = ((Binding)comboCol.SelectedValueBinding).Path.Path;
                        }
                        else if (!string.IsNullOrEmpty(comboCol.SelectedValuePath))
                        {
                            propertyName = comboCol.SelectedValuePath;
                        }
                        else if (!string.IsNullOrEmpty(comboCol.DisplayMemberPath))
                        {
                            propertyName = comboCol.DisplayMemberPath;
                        }
    
                        var value = item.GetType().GetProperty(
                             propertyName).GetValue(
                                item,
                                new object[] { });
    
                        if (comboCol.SelectedValueBinding != null
                            && ((Binding)comboCol.SelectedValueBinding).Converter != null)
                        {
                            var bnd = (Binding)comboCol.SelectedValueBinding; 
                            value
                                = bnd.Converter.Convert(
                                    value,
                                    typeof(object),
                                    bnd.ConverterParameter,
                                    bnd.ConverterCulture);
                        }
                        gridContent = gridContent + "\t" + value.ToString();
                    }
                    if (templateCol != null)
                    {
                        var propertyName = templateCol.SortMemberPath;
                        var value
                            = item.GetType().GetProperty(
                                 propertyName).GetValue(
                                   item,
                                   new object[] { });
    
                        gridContent = gridContent + "\t" + value.ToString();
                    }
                }
    
                gridContent = gridContent + "\n";
            }
    
            MessageBox.Show(gridContent);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My scenario is such that I have a VB.NET project in SVN and I
have such zend query: $select = $this->_table ->select() ->where('title LIKE ?', '%'.$searchWord.'%') ->where('description LIKE
I have such a form: <form id=new-application method=post enctype=multipart/form-data data-remote=true action=/inv/claims?locale=uk accept-charset=UTF-8> <a rel=nofollow
I have such a link in JSP page with encoding big5 http://hello/world?name=婀ㄉ And when
I have such a class: public class Cycle { public List<int> Edges { get;
I have such class public unsafe class EigenSolver { public double* aPtr {get; private
I have this scenario. I need to show 3 list boxes on a ASP.NET
I know that somewhere in the history of the internet this must have been
I have an asp.net page that is trying to access a SSRS 2008 ReportServer
Scenario: I have a web page with multiple charts and I have an 'Export

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.