SIGN UP MEMBER LOGIN:    
ARTICLE

Fetch Data From GridView and Store into DataTable

Posted by Ankit Nandekar Articles | ADO.NET in C# April 13, 2011
This article demonstrates how to fetch data from a GridView and how to store it into a DataTable.
Reader Level:


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.

Login to add your contents and source code to this article
share this article :
post comment
 

thnx

Posted by Ankit Nandekar Apr 14, 2011

Very Useful article.......

Posted by Dinesh Beniwal Apr 14, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor