Asp.net GridView



The grid displays the value of a data source in the visual control table. Each column represents a field, while each row represents a record grid view control supports the following features:


  • Binding for data source controls, such as SqlDataSource
  • Built-in sort capabilities
  • Remove the underlying updates and capabilities.
  • Built-in paging capabilities
  • Built-in line selection capabilities
  • Programmatic access to dynamically set properties up to gridview object models, handle events, and so on.
  • Multiple key fields
  • Multiple data fields for hyperlink columns
  • Adaptive appearance through themes and styles
Syntax - 

<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

Image result for Asp.net GridView

Gridview.aspx 

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

Gridview.aspx.cs

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

Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment