Click here to Skip to main content
11,705,610 members (52,005 online)
Click here to Skip to main content

GridView to a DataTable

, 6 Aug 2006 CPOL 163.3K 36
Rate this:
Please Sign up or sign in to vote.
Convert the current Gridview to a datatable

The Problem

SQL Data source retrieves data from the server and bind it to the Gridview, what if the data you need for some reason "that what I have faced when I needed to bid the chart to grid shown data" is the shown data in the Gridview, the data that the Gridview hold now, here is the method to do so:

Solution

  1. get the a data view from the SQL data source
  2. DataView dataView;dataView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    
  3. check if the data view is not null
  4. if (ChartDataView != null) {}
  5. Set the structure of the table from the SQL data source
  6. DataTable dt;
    dt = ChartDataView.Table.Clone();
  7. Copy the GridViewRows to an array
  8.   GridViewRow[] Chartarr = new ridViewRow[GridViewData.Rows.Count];
    GridViewData.Rows.CopyTo(Chartarr, 0)
    
  9. Add the values to the new data table:
  10. foreach (GridViewRow row in Chartarr)
                 {
                     DataRow datarw;
                     datarw = dt.NewRow();
                     for (int i = 0; i < row.Cells.Count; i++)
                     {
                         datarw[i] = row.Cells[i].Text;
                     }
     
                     dt.Rows.Add(datarw);
             }

The Complete Code

if (ChartDataView != null) 
{

DataView dataView;

dataView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); 

DataTable dt;
dt = ChartDataView.Table.Clone();
 
GridViewRow[] Chartarr = new ridViewRow[GridViewData.Rows.Count];
  GridViewData.Rows.CopyTo(Chartarr, 0)

foreach (GridViewRow row in Chartarr)
             {
                 DataRow datarw;
                 datarw = dt.NewRow();
                 for (int i = 0; i < row.Cells.Count; i++)
                 {
                     datarw[i] = row.Cells[i].Text;
                 }
 
                 dt.Rows.Add(datarw);
         }
}

License

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

Share

About the Author

ZeroDev
Chief Technology Officer
Jordan Jordan
Life is really simple, but we insist on making it complicated

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
sharadverma_VNS21-May-13 0:01
membersharadverma_VNS21-May-13 0:01 
GeneralMy vote of 5 Pin
zohirey228-Feb-11 4:21
memberzohirey228-Feb-11 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 6 Aug 2006
Article Copyright 2006 by ZeroDev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid