Click here to Skip to main content
Licence 
First Posted 6 Aug 2006
Views 102,585
Bookmarked 32 times

GridView to a DataTable

By | 6 Aug 2006 | Article
Convert the current Gridview to a datatable
 
Part of The SQL Zone sponsored by
See Also
Introduction

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

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

2. check if the data view is not null

 

if (ChartDataView != null) {}

3. Set the structure of the table from the SQL data source

 
DataTable dt;
dt = ChartDataView.Table.Clone();

4.Copy the GridViewRows to an array

    GridViewRow[] Chartarr = new ridViewRow[GridViewData.Rows.Count];
  GridViewData.Rows.CopyTo(Chartarr, 0)

5. Add the values to the new data table:

 
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 Complet code

 
if (ChartDataView != null) 


{


DataView dataView;


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


<pre class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; DIRECTION: ltr; unicode-bidi: embed; TEXT-ALIGN: left">DataTable dt;
dt = ChartDataView.Table.Clone();
 
GridViewRow[] Chartarr = new ridViewRow[GridViewData.Rows.Count];
<pre class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; DIRECTION: ltr; unicode-bidi: embed; TEXT-ALIGN: left">  GridViewData.Rows.CopyTo(Chartarr, 0)

<pre class="MsoNormal" style="MARGIN: 0in 0in 0pt; DIRECTION: ltr; unicode-bidi: embed; TEXT-ALIGN: left; mso-layout-grid-align: none">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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ZeroDev

Chief Technology Officer

Jordan Jordan

Member

Life is really simple, but we insist on making it complicated

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberzohirey24:21 28 Feb '11  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 6 Aug 2006
Article Copyright 2006 by ZeroDev
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid