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" /> 
                      <asp:TemplateField HeaderText="Book Iamge">
                          <asp:Image ID="BookImg" ImageUrl='<%# Eval("BookImage") %>' runat="server" hight="15px" width="20px"/>


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


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


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

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

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

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();

        Session["QtyTable"] = dt;

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.


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now