Click here to Skip to main content
16,015,756 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi,

I have a requirement to copy GridView data to Datatable.

My code is below:--

.aspx:--

XML
<div>
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false" >
      <Columns>


                    <asp:TemplateField HeaderText="Book Name">
                       <ItemTemplate>
                           <%#Eval("BookName")%>

                       </ItemTemplate>
                       </asp:TemplateField>

                    <asp:TemplateField HeaderText="Price">
                       <ItemTemplate>
                           <%#Eval("Bookprice")%>
                       </ItemTemplate>

                   </asp:TemplateField>






               </Columns>
      </asp:GridView>
  </div>


.aspx.cs:---


C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("BookName"), new DataColumn("BookPrice") });
            dt1.Rows.Add("khan Hammond", "10.00");
            dt1.Rows.Add("Mojibar", "20.50");
            dt1.Rows.Add("Saean Mathews", "11.25");
            dt1.Rows.Add("Robin Schidner", "20.35");
            Session["QtyTable"] = dt1;
            GridView1.DataSource = dt1;
            GridView1.DataBind();
        }
    }


private void getGridInfo()
{

DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("BookName", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("BookPrice", typeof(String)));
for (int i = 0; i < GridView1.Rows.Count; i++)
{
dr[i] = GridView1.Rows[0].Cells[0].Text;
dr[i + 1] = GridView1.Rows[i].Cells[i].Text;
dt.Rows.Add(dr);
}
}

Now when I see the datatable,its blank.

Please post suitable code...

Thanks...
Posted
Updated 22-Jan-14 2:04am
v3
Comments
Have you debugged and seen it is working or not?

just cast the Gridview to datatable as below
may it will help you

C#
private void getGridInfo()
{
   DataTable dt = (DataTable)GridView1.DataSource;
}
 
Share this answer
 
use this code..


C#
private void getGridInfo()
        {

            DataTable dt = new DataTable();
           
            dt.Columns.Add(new System.Data.DataColumn("BookName", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("BookPrice", typeof(String)));
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                DataRow dr = dt.NewRow(); 
                var row = GridView1.Rows[i];
                dr[0]  = (row.Cells[0].Controls[0] as DataBoundLiteralControl).Text;
                dr[1] = (row.Cells[1].Controls[0] as DataBoundLiteralControl).Text;
               
                dt.Rows.Add(dr);
            }
        }
 
Share this answer
 
Comments
rupai99 22-Jan-14 8:31am    
Many many thanks dude....
Karthik_Mahalingam 22-Jan-14 8:32am    
welcome rupai :)

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