Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all

i want to export grid view to datatable and export that datatable to another gridview.
my code is
C#
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
Updated 6-Jun-12 0:02am
v3
Comments
AmitGajjar 6-Jun-12 6:45am    
on which line you are getting error ? have you checked your gv1 is not null ?
Ragi Gopi 6-Jun-12 6:46am    
yes....
iam getting error in this line
dr[i] = row.Cells[j].Text;
thanks for responding
Raza_theraaz 23-Feb-13 6:09am    
none of the solution here helped me.

you can convert gridview data to a datatable using this function.

Code Here:
C#
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;
        }
 
Share this answer
 
Comments
Ragi Gopi 7-Jun-12 1:20am    
thanks csharpbd
jomytrs 2-Sep-12 3:40am    
Message = "A column named ' ' already belongs to this DataTable.
csharpbd 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 12-Feb-13 3:07am    
where to put that code when i put that function on my button it show me an error
csharpbd 12-Feb-13 5:24am    
Please explain when you got error, also what is the error?
instead of this you can do like this

DataTable dt = (DataTable)gv1.DataSource;


then you can assign dt to another gridview
 
Share this answer
 
Comments
sumitkmr558 18-Aug-12 2:10am    
DataTable dt = (DataTable)gv1.DataSource;
This code don't pick gridview data source
Omar Martínez 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
Member 13096349 20-Jun-18 3:35am    
not working
GridView to a DataTable[^]

i think it is useful for you
 
Share this answer
 
v2
Comments
Ragi Gopi 6-Jun-12 7:10am    
DataTable dt;
dt = ChartDataView.Table.Clone();


there is no attribute called table for gridview in asp.net
Ragi Gopi 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900