GridView Control in ASP.Net

This article shows how to use a GridView control in ASP.Net using C# code behind.

Introduction

 
The GridView control displays the values of a data source in a table. Each column represents a field, while each row represents a record. The GridView control supports the following features:
  • Binding to data source controls, such as SqlDataSource.
  • Built-in sort capabilities.
  • Built-in update and delete capabilities.
  • Built-in paging capabilities.
  • Built-in row selection capabilities.
  • Programmatic access to the GridView object model to dynamically set properties, handle events, and so on.
  • Multiple key fields.
  • Multiple data fields for the hyperlink columns.
  • Customizable appearance through themes and styles.

Creating a GridView

  1. <asp:GridView ID="gridService" runat="server">  
  2. </asp:GridView>  
This article shows how to use a GridView control in ASP.Net using C# code behind. In this we perform the following operations on GridView.
  • Bind data to GridView column
  • Edit data in GridView
  • Delete rows from GridView
  • Update row from database
I have a sample example that explains all the preceding operations.
 
GridView Control 
 
HTML Code for generating GridView UI
  1. <div id="panel" style="height: 500px; background-color: White; padding: 10px; overflow: auto">  
  2.      <h1>  
  3.         <a href="../adminIndex.aspx">Back </a>| Service Master  
  4.     </h1>  
  5.     <asp:updatepanel id="UpdatePanelService" runat="server" updatemode="Conditional">  
  6.             <ContentTemplate>  
  7.                 <asp:GridView ID="gridService" runat="server" CssClass="EU_DataTable" AutoGenerateColumns="false" ShowFooter="true"OnRowEditing="gridService_RowEditing"            onrowcreated="gridService_RowCreated"onrowupdating="gridService_RowUpdating">  
  8.                     <Columns>  
  9.                         <asp:TemplateField ItemStyle-Width="30px" HeaderText="SR.NO">  
  10.                             <ItemTemplate>  
  11.                                 <asp:Label ID="lblID" runat="server"  
  12.                                   Text='<%#Eval("service_id")%>'></asp:Label>  
  13.                             </ItemTemplate>  
  14.                         </asp:TemplateField>  
  15.                         <asp:TemplateField ItemStyle-Width="600px" HeaderText="Service">  
  16.                             <ItemTemplate>  
  17.                                 <asp:Label ID="lblService" runat="server" Text='<%#Eval("service_name")%>'></asp:Label>  
  18.                             </ItemTemplate>  
  19.                             <EditItemTemplate>  
  20.                                 <asp:TextBox ID="txtService" runat="server" Text='<%#Eval("service_name")%>'></asp:TextBox>  
  21.                             </EditItemTemplate>  
  22.                             <FooterTemplate>  
  23.                                 <asp:TextBox ID="txtService" runat="server"></asp:TextBox>  
  24.                             </FooterTemplate>  
  25.                         </asp:TemplateField>  
  26.                         <asp:TemplateField ItemStyle-Width="100px" HeaderText="Service Photo">  
  27.                             <ItemTemplate>  
  28.                                 <img src='<%# Eval("service_image")%>' alt='<%#  
  29.                                                 Eval("service_image")%>height="50px"  
  30.                                     width="50px" />  
  31.                             </ItemTemplate>  
  32.                             <EditItemTemplate>  
  33.                                 <asp:FileUpload ID="fuService" runat="server" />  
  34.                             </EditItemTemplate>  
  35.                             <FooterTemplate>  
  36.                                 <asp:FileUpload ID="fuService" runat="server" />  
  37.                             </FooterTemplate>  
  38.                         </asp:TemplateField>  
  39.                         <asp:TemplateField>  
  40.                             <ItemTemplate>  
  41.                                 <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%#  
  42.                                          Eval("service_id")%>'  
  43.                                     OnClientClick="return confirm('Do you want to delete?')"  
  44.                                   Text="Delete"></asp:LinkButton>  
  45.                             </ItemTemplate>  
  46.                             <FooterTemplate>  
  47.                                 <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddService"/>  
  48.                             </FooterTemplate>  
  49.                         </asp:TemplateField>  
  50.                         <asp:CommandField ShowEditButton="True" />  
  51.                     </Columns>  
  52.                 </asp:GridView>  
  53.                 <asp:UpdateProgress ID="UpdateProgress1" runat="server"  
  54.                      AssociatedUpdatePanelID="UpdatePanelService">  
  55.                     <ProgressTemplate>  
  56.                         Please wait image is getting uploaded....  
  57.                     </ProgressTemplate>  
  58.                 </asp:UpdateProgress>  
  59.             </ContentTemplate>  
  60.             <Triggers>  
  61.             </Triggers>  
  62.        </asp:updatepanel>  
  63. </div>  

Binding GridView

  1. private void _BindService()  
  2. {  
  3.     try  
  4.     {  
  5.         List<BOService> service = dALService.Service.ToList();  
  6.         if (service.Count > 0 && service != null)  
  7.         {  
  8.             gridService.DataSource = service;  
  9.             gridService.DataBind();  
  10.        }  
  11.    }  
  12.    catch (Exception)  
  13.    {  
  14.        throw;  
  15.    }  
  16. }  

Editing GridView

  1. protected void gridService_RowEditing(object sender, GridViewEditEventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         gridService.EditIndex = e.NewEditIndex;  
  6.         _BindService();  
  7.     }  
  8.     catch (Exception)  
  9.     {   
  10.         throw;  
  11.     }  
  12. }  

Updating GridView

  1. protected void gridService_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         string servicename = ((TextBox)gridService.Rows[e.RowIndex].FindControl("txtService")).Text;  
  6.         string filePath =((FileUpload)gridService.Rows[e.RowIndex].FindControl("fuService")).FileName;  
  7.         bOService.Service_name = servicename;  
  8.         bOService.Service_image = "../images/service/" + filePath;  
  9.         if (File.Exists(Server.MapPath("~/images/service/" + filePath)))  
  10.         {  
  11.         }  
  12.         else  
  13.         {
  14. ((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));  
  15.         }  
  16.         dALService.UpdateService(bOService);  
  17.         _BindService();  
  18.     }  
  19.     catch (Exception)  
  20.     {  
  21.         throw;  
  22.     }  
  23. }  

Adding value in GridView

  1. protected void AddService(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         string servicename = ((TextBox)gridService.FooterRow.FindControl("txtService")).Text;  
  6.         string filePath = ((FileUpload)gridService.FooterRow.FindControl("fuService")).FileName;  
  7.         bOService.Service_name = servicename;  
  8.         bOService.Service_image = "../images/service/" + filePath;  
  9.         if (File.Exists(Server.MapPath("~/images/service/" + filePath)))  
  10.         {  
  11.         }  
  12.         else  
  13.         {
  14. ((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));  
  15.         }
  16. ((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));  
  17.         dALService.SetService(bOService);  
  18.         _BindService();  
  19.     }  
  20.     catch (Exception)  
  21.     {  
  22.         throw;  
  23.     }  
  24. }  

Summary

 
This article has provided an overview of how to use an asp:GridView completely, and how to use RowEditing, RowUpdating, RowDeleting, RowCommand, RowDataBound, RowCancelingEdit, and Pagination in a DataGrid. From this article, you will have a clear view of the GridView data insert, delete, and update operations.