GridView Control in ASP.Net

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

<asp:GridView ID="gridService" runat="server">
</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
 

<div id="panel" style="height: 500px; background-color: White; padding: 10px; overflow: auto">

     <h1>

        <a href="../adminIndex.aspx">Back </a>| Service Master

    </h1>

    <asp:updatepanel id="UpdatePanelService" runat="server" updatemode="Conditional">

            <ContentTemplate>

                <asp:GridView ID="gridService" runat="server" CssClass="EU_DataTable"

                     AutoGenerateColumns="false" ShowFooter="true"OnRowEditing="gridService_RowEditing"

                    onrowcreated="gridService_RowCreated"onrowupdating="gridService_RowUpdating">

                    <Columns>

                        <asp:TemplateField ItemStyle-Width="30px" HeaderText="SR.NO">

                            <ItemTemplate>

                                <asp:Label ID="lblID" runat="server"

                                  Text='<%#Eval("service_id")%>'></asp:Label>

                            </ItemTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField ItemStyle-Width="600px" HeaderText="Service">

                            <ItemTemplate>

                                <asp:Label ID="lblService" runat="server" Text='<%#

                                         Eval("service_name")%>'></asp:Label>

                            </ItemTemplate>

                            <EditItemTemplate>

                                <asp:TextBox ID="txtService" runat="server" Text='<%#

                                         Eval("service_name")%>'></asp:TextBox>

                            </EditItemTemplate>

                            <FooterTemplate>

                                <asp:TextBox ID="txtService" runat="server"></asp:TextBox>

                            </FooterTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField ItemStyle-Width="100px" HeaderText="Service Photo">

                            <ItemTemplate>

                                <img src='<%# Eval("service_image")%>' alt='<%#

                                                Eval("service_image")%>' height="50px"

                                    width="50px" />

                            </ItemTemplate>

                            <EditItemTemplate>

                                <asp:FileUpload ID="fuService" runat="server" />

                            </EditItemTemplate>

                            <FooterTemplate>

                                <asp:FileUpload ID="fuService" runat="server" />

                            </FooterTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField>

                            <ItemTemplate>

                                <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%#

                                         Eval("service_id")%>'

                                    OnClientClick="return confirm('Do you want to delete?')"

                                  Text="Delete"></asp:LinkButton>

                            </ItemTemplate>

                            <FooterTemplate>

                                <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddService"

                                         />

                            </FooterTemplate>

                        </asp:TemplateField>

                        <asp:CommandField ShowEditButton="True" />

                    </Columns>

                </asp:GridView>

                <asp:UpdateProgress ID="UpdateProgress1" runat="server"

                     AssociatedUpdatePanelID="UpdatePanelService">

                    <ProgressTemplate>

                        Please wait image is getting uploaded....

                    </ProgressTemplate>

                </asp:UpdateProgress>

            </ContentTemplate>

            <Triggers>

          

            </Triggers>

       </asp:updatepanel>

</div>


Binding GridView
 

private void _BindService()

{

    try

    {

        List<BOService> service = dALService.Service.ToList();

        if (service.Count > 0 && service != null)

        {

            gridService.DataSource = service;

            gridService.DataBind();

       }

   }

   catch (Exception)

   {

 

       throw;

   }

}


Editing GridView

protected void gridService_RowEditing(object sender, GridViewEditEventArgs e)

{

    try

    {

        gridService.EditIndex = e.NewEditIndex;

        _BindService();

    }

    catch (Exception)

    { 

        throw;

    }

}

Updating GridView
 

protected void gridService_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

    try

    {

        string servicename = ((TextBox)gridService.Rows[e.RowIndex].FindControl("txtService")).Text;

        string filePath =((FileUpload)gridService.Rows[e.RowIndex].FindControl("fuService")).FileName;

        bOService.Service_name = servicename;

        bOService.Service_image = "../images/service/" + filePath;

        if (File.Exists(Server.MapPath("~/images/service/" + filePath)))

        {

 

        }

        else

        {

            ((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));

        }

        dALService.UpdateService(bOService);

        _BindService();

    }

    catch (Exception)

    {

        throw;

    }

 }

Adding value in GridView
 

protected void AddService(object sender, EventArgs e)

{

    try

    {

        string servicename = ((TextBox)gridService.FooterRow.FindControl("txtService")).Text;

        string filePath = ((FileUpload)gridService.FooterRow.FindControl("fuService")).FileName;

        bOService.Service_name = servicename;

        bOService.Service_image = "../images/service/" + filePath;

        if (File.Exists(Server.MapPath("~/images/service/" + filePath)))

        {

        }

        else

        {

            ((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));

        }

        ((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));

        dALService.SetService(bOService);

        _BindService();

    }

    catch (Exception)

    {

        throw;

    }

}
 
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.

Up Next
    Ebook Download
    View all
    Learn
    View all