Fetch Data From GridView and Store into DataTable



This article demonstrates how to fetch data from a GridView and how to store it into a DataTable.

Follow these simple steps:

Step 1:

Drag a GridView onto the form and set it's AutoGenerateColumns property to False.

Use Template Fields to display GridView data. Design the GridView as per your reqirements.

<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="False"
                 EmptyDataText="There is nothing in your shopping cart." GridLines="None"
                 Width="31%" CellPadding="5" ShowFooter="True" DataKeyNames="BookId" onrowdeleting="gvShoppingCart_RowDeleting" 
                  > 
                 <HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" /> 
                 <FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" /> 
                 <AlternatingRowStyle BackColor="#F8F8F8" /> 
                 <Columns> 
                      <asp:TemplateField HeaderText="Book Iamge">
                      <ItemTemplate>
                          <asp:Image ID="BookImg" ImageUrl='<%# Eval("BookImage") %>' runat="server" hight="15px" width="20px"/>

                      </ItemTemplate>
                      </asp:TemplateField>

                      <asp:TemplateField HeaderText="Book Name"> 
                         <ItemTemplate> 
                             <asp:Label ID="lblBookName" runat="server" Text='<%#Eval("BookName")%>'></asp:Label><br /> 

                         </ItemTemplate>
                         </asp:TemplateField>

                      <asp:TemplateField HeaderText="Price"> 
                         <ItemTemplate> 
                             <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Bookprice")%>'></asp:Label><br /> 
                         </ItemTemplate>

                     </asp:TemplateField> 

                   <asp:TemplateField HeaderText="Quantity">
                  <ItemTemplate>
                      <asp:TextBox ID="TXTQty" runat="server"  Text="1" Width="40px" onkeyup="CalculateTotals();"></asp:TextBox>
                  </ItemTemplate>
                  <FooterTemplate>
                      <b>Total Amount:</b>
                  </FooterTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="Total">
                  <ItemTemplate>
                      <asp:Label ID="LBLSubTotal" runat="server" ></asp:Label>

</ItemTemplate>
                  <FooterTemplate>
                      <asp:Label ID="LBLTotal" runat="server" ForeColor="White" ></asp:Label>
                  </FooterTemplate>
              </asp:TemplateField>

                     <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
                    
                 </Columns> 
             </asp:GridView>

Here in Eval pass your table column name .

Step 2:

Use the following code to fetch data from a GridView and store it into a DataTable.

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("BookQty", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("BookImg", typeof(String)));

        foreach (GridViewRow row in gvShoppingCart.Rows)
        {
            Image Bookimg = (Image)row.FindControl("BookImg");
            Label Booknames = (Label)row.FindControl("lblBookName");
            TextBox Bookqty = (TextBox)row.FindControl("TXTQty");
            Label TotalPrice = (Label)row.FindControl("LBLTotal");
            dr = dt.NewRow();
            dr[0] = Booknames.Text;
            dr[1] = Bookqty.Text;
            dr[2] = Bookimg.ImageUrl.ToString();
            dt.Rows.Add(dr);
        }

        Session["QtyTable"] = dt;
        Response.Redirect("Admin/Default.aspx");
    }


Step 3:

Here in dt.Columns.Add(new System.Data.DataColumn("BookName", typeof(String))) you can write any header text for the column.


Call this function where you want to store a GridView data into a DataTable.

Up Next
    Ebook Download
    View all
    Learn
    View all