Web
Analytics
Edition in Grid View Control | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

In This Video tutorial I demonstrate that how we can edit content in grid view itself.

GridViewEdit.aspx

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”
            CellPadding=”4″ ForeColor=”#333333″ GridLines=”None”
            onrowdeleting=”GridView1_RowDeleting” onrowediting=”GridView1_RowEditing”
            onrowcancelingedit=”GridView1_RowCancelingEdit”
            onrowupdating=”GridView1_RowUpdating”>
            <AlternatingRowStyle BackColor=”White” ForeColor=”#284775″ />
            <Columns>
                <asp:TemplateField HeaderText=”ID”>
                    <EditItemTemplate>
                        <asp:Label ID=”Label5″ runat=”server” Text='<%# Eval(“id”) %>’></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID=”Label1″ runat=”server” Text='<%# Eval(“id”) %>’></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=”User Name”>
                    <EditItemTemplate>
                        <asp:TextBox ID=”TextBox1″ runat=”server” Text='<%# Eval(“name”) %>’></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID=”Label2″ runat=”server” Text='<%# Eval(“name”) %>’></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=”Father Name”>
                    <EditItemTemplate>
                        <asp:TextBox ID=”TextBox2″ runat=”server” Text='<%# Eval(“fathername”) %>’></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID=”Label3″ runat=”server” Text='<%# Eval(“FatherName”) %>’></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=”Salary”>
                    <EditItemTemplate>
                        <asp:TextBox ID=”TextBox3″ runat=”server” Text='<%# Eval(“salary”) %>’></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID=”Label4″ runat=”server” Text='<%# Eval(“salary”) %>’></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField HeaderText=”Deletion” ShowDeleteButton=”True” />
                <asp:CommandField HeaderText=”Edition” ShowEditButton=”True” />
            </Columns>
            <EditRowStyle BackColor=”#999999″ />
            <FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
            <HeaderStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
            <PagerStyle BackColor=”#284775″ ForeColor=”White” HorizontalAlign=”Center” />
            <RowStyle BackColor=”#F7F6F3″ ForeColor=”#333333″ />
            <SelectedRowStyle BackColor=”#E2DED6″ Font-Bold=”True” ForeColor=”#333333″ />
            <SortedAscendingCellStyle BackColor=”#E9E7E2″ />
            <SortedAscendingHeaderStyle BackColor=”#506C8C” />
            <SortedDescendingCellStyle BackColor=”#FFFDF8″ />
            <SortedDescendingHeaderStyle BackColor=”#6F8DAE” />
        </asp:GridView>

GridViewEdit.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class GridViewedit : System.Web.UI.Page
{
       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“conn”].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillgrid();
        }
    }
    public void fillgrid()
    {
        SqlDataAdapter da = new SqlDataAdapter(“sp_GetData”, con);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = ((Label)GridView1.Rows[e.RowIndex].FindControl(“Label1”)).Text;
        SqlCommand cmd = new SqlCommand(“sp_Delete”, con);
        cmd.Parameters.Add(new SqlParameter(“@id”,id));
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result > 0)
        {
            fillgrid();

        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.PageIndex = e.NewEditIndex;
        fillgrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        fillgrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = ((Label)GridView1.Rows[e.RowIndex].FindControl(“Label5”)).Text;
        string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl(“TextBox1”)).Text;
        string fname = ((TextBox)GridView1.Rows[e.RowIndex].FindControl(“TextBox2”)).Text;
        string salary = ((TextBox)GridView1.Rows[e.RowIndex].FindControl(“TextBox3”)).Text;
        SqlCommand cmd = new SqlCommand(“sp_update”, con);
        cmd.Parameters.Add(new SqlParameter(“@id”, id));
        cmd.Parameters.Add(new SqlParameter(“@name”, name));
        cmd.Parameters.Add(new SqlParameter(“@fathername”,fname));
        cmd.Parameters.Add(new SqlParameter(“@salary”,salary));
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result > 0)
        {
            GridView1.EditIndex = -1;
            fillgrid();

        }
    }
}

Download Source Code