Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
hi all
 
i want to export grid view to datatable and export that datatable to another gridview.
my code is
DataTable dt = new DataTable();
        for (int j = 0; j < gv1.Rows.Count; j++)
        {
            DataRow dr;
            GridViewRow row = gv1.Rows[j];
            dr = dt.NewRow();
            for (int i = 0; i < row.Cells.Count; i++)
            {
                dr[i] = row.Cells[j].Text;
            }
 
            dt.Rows.Add(dr);
        }
        gv2.DataSource = dt;
        gv2.DataBind();
 
iam getting the following error
 

 
[IndexOutOfRangeException: Cannot find column 0.]
   System.Data.DataColumnCollection.get_Item(Int32 index) +95
   System.Data.DataRow.set_Item(Int32 columnIndex, Object value) +15
   MPR_mpr.bt_as_Click(Object sender, EventArgs e) in f:\workson\Virtual_Office_KFRI\MPR\mpr.aspx.cs:145
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
 

 

thank u you alll
Posted 5-Jun-12 23:53pm
Ragi Gopi1.3K
Edited 6-Jun-12 0:02am
v3
Comments
amitgajjar at 6-Jun-12 6:45am
   
on which line you are getting error ? have you checked your gv1 is not null ?
Ragi Gopi at 6-Jun-12 6:46am
   
yes....
iam getting error in this line
dr[i] = row.Cells[j].Text;
thanks for responding
raza.ahmed.se at 23-Feb-13 6:09am
   
none of the solution here helped me.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

you can convert gridview data to a datatable using this function.
 
Code Here:
DataTable GetDataTable(GridView dtg)
        {
            DataTable dt = new DataTable();
 
            // add the columns to the datatable            
            if (dtg.HeaderRow != null)
            {
 
                for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
                {
                    dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
                }
            }
 
            //  add each of the data rows to the table
            foreach (GridViewRow row in dtg.Rows)
            {
                DataRow dr;
                dr = dt.NewRow();
 
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    dr[i] = row.Cells[i].Text.Replace(" ", "");
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
  Permalink  
Comments
Ragi Gopi at 7-Jun-12 1:20am
   
thanks csharpbd
jomytrs at 2-Sep-12 3:40am
   
Message = "A column named ' ' already belongs to this DataTable.
csharpbd at 3-Sep-12 15:16pm
   
there any column with blank header in your gridview, if exist and it's require for you please set a header or remove that column from your gridview.
Amitesh17 at 12-Feb-13 3:07am
   
where to put that code when i put that function on my button it show me an error
csharpbd at 12-Feb-13 5:24am
   
Please explain when you got error, also what is the error?
Amitesh17 at 12-Feb-13 5:54am
   
same error cannot find column 0 by using the ragi gopi command and when i put your func: GetDataTable on button click event it show Error
2 } expected E:\Visual Studio 2010\Projects\Amitesh\WebBased\Default7.aspx.cs 69 6 E:\...\WebBased\
Amitesh17 at 12-Feb-13 6:01am
   
now when i use ur code it showing this error:
 
Object reference not set to an instance of an object.
Amitesh17 at 12-Feb-13 6:11am
   
i guess u got my point::
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

instead of this you can do like this
 
DataTable dt = (DataTable)gv1.DataSource;
 
then you can assign dt to another gridview
  Permalink  
Comments
sumitkmr558 at 18-Aug-12 2:10am
   
DataTable dt = (DataTable)gv1.DataSource;
This code don't pick gridview data source
Omar Martínez at 28-Jun-13 16:41pm
   
This is perfect. Just erase the (DataTable) part and it should work:
DataTable dt = gv1.DataSource;
 
On VB.NET:
Dim dt As DataTable = gv1.DataSource
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

GridView to a DataTable[^]
 
i think it is useful for you
  Permalink  
v2
Comments
Ragi Gopi at 6-Jun-12 7:10am
   
DataTable dt;
dt = ChartDataView.Table.Clone();
 

there is no attribute called table for gridview in asp.net
Ragi Gopi at 6-Jun-12 7:10am
   
how can i over come that..?

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 853
1 OriginalGriff 410
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,482
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 6 Jun 2012
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