Populate Datatable From Filtered Gridview

7 posts, 2 answers
  1. Chris Lynch
    Chris Lynch avatar
    15 posts
    Member since:
    Mar 2010

    Posted 11 Sep 2010 Link to this post

    Is there an easy way to populate a Datatable from a Filtered gridview?
  2. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 16 Sep 2010 Link to this post

    Hi Chris Lynch,

    Yes, this is possible. However, you should do it manually by iterating all rows. Here is a sample:
    DataTable table = new DataTable();
    foreach (GridViewDataColumn column in this.radGridView1.Columns)
    {
        table.Columns.Add(column.Name, column.DataType);
    }
    foreach (GridViewRowInfo row in this.radGridView1.Rows)
    {
        DataRow dataRow = table.NewRow();
        for (int i = 0; i < table.Columns.Count; i++)
        {
            dataRow[i] = row.Cells[i].Value;
        }
        table.Rows.Add(dataRow);
    }

    I hope it helps.

    Greetings, Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Chris Lynch
    Chris Lynch avatar
    15 posts
    Member since:
    Mar 2010

    Posted 17 Sep 2010 Link to this post

    thanks
  5. george mcnitt
    george mcnitt avatar
    14 posts
    Member since:
    May 2010

    Posted 02 Dec 2010 Link to this post

    this currently isnt working in the current version of the grid control. It returns all the rows not just the filtered ones.

    is there any update on how to get only the filtered rows?
  6. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 02 Dec 2010 Link to this post

    Hello George,

    this was changed in the Q3 2010 Release. An extract from the Q3 release notes reads:

  7. BREAKING CHANGE: The Rows collection of GridViewTemplate now contains all bound/unbound rows. The filtered, grouped, and sorted lists of rows can be accessed from the ChildRows collection of GridViewTemplate or from every GridViewRowInfo in a hierarchical view of rows.

    and here is the link to the Q3 Release notes

    Hope that helps
    Richard

     

  • Answer
    Ryan
    Ryan avatar
    22 posts
    Member since:
    Oct 2010

    Posted 02 Dec 2010 Link to this post

    Here's a quick example

    Dim table As New DataTable()
    For Each column As GridViewDataColumn In Me.grdClientSummary.Columns
        table.Columns.Add(column.Name, column.DataType)
    Next
     
    For Each oChildItem In grdClientSummary.TableElement.ViewInfo.ChildRows
        Dim dataRow As DataRow = table.NewRow()
        For x As Integer = 0 To table.Columns.Count - 1
            DataRow(x) = oChildItem.Cells(x).Value
        Next
        table.Rows.Add(dataRow)
    Next
  • Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 07 Dec 2010 Link to this post

    Hi All,

    You can use ChildRows for this scenario just like Ryan said:

    DataTable table = new DataTable();
    foreach (GridViewDataColumn column in this.radGridView1.Columns)
    {
        table.Columns.Add(column.Name, column.DataType);
    }
     
    foreach (GridViewRowInfo row in this.radGridView1.ChildRows)
    {
        DataRow dataRow = table.NewRow();
        for (int i = 0; i < table.Columns.Count; i++)
        {
            dataRow[i] = row.Cells[i].Value;
        }
        table.Rows.Add(dataRow);
    }
     
    Please keep in mind that ChildRows represents the hierarchical view of Rows and when you have a grouping operation applied, the root level will contains only Group rows.

    If you want to get only the filtered and sorted rows in a flat view without groping, you must use the DataView property of GridViewTemplate in this case:
    DataTable table = new DataTable();
    foreach (GridViewDataColumn column in this.radGridView1.Columns)
    {
        table.Columns.Add(column.Name, column.DataType);
    }
     
    foreach (GridViewRowInfo row in this.radGridView1.MasterTemplate.DataView)
    {
        DataRow dataRow = table.NewRow();
        for (int i = 0; i < table.Columns.Count; i++)
        {
            dataRow[i] = row.Cells[i].Value;
        }
        table.Rows.Add(dataRow);
    }

    I hope this helps.

    Kind regards,
    Julian Benkov
    the Telerik team
    Get started with RadControls for WinForms with numerous videos and detailed documentation.
  • Back to Top
    UI for WinForms is Visual Studio 2017 Ready