Hi Guys,
This blog we discuss how to edit gridview
using asp.net
Gridgrid.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridgrid.aspx.cs" Inherits="gridgrid" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Font-Bold="False">
<AlternatingRowStyle BackColor="NavajoWhite" />
<Columns>
<asp:TemplateField HeaderText="No_Id">
<ItemTemplate>
<%#Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpId">
<ItemTemplate>
<asp:Label ID="Empid" runat="server" Text='<%#Eval("EmpId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<%#Eval("City") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlcity" DataTextField="City" DataSource='<%#Fetchdata("City") %>' runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="editit" ImageUrl="~/Images/edit_it.jpg" runat="server" Height="30" Width="30" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="cancelit" ImageUrl="~/Images/cancel.jpg" runat="server" Height="30" Width="30" CommandName="Cancel" />
<asp:ImageButton ID="updateit" ImageUrl="~/Images/up.jpg" runat="server" Height="30" Width="30" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#3333FF" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
Gridgrid.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.SqlClient;
using System.Configuration;
using System.Data;
public partial class gridgrid : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sai"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindbind();
}
}
protected void bindbind()
{
SqlDataAdapter sdr = new SqlDataAdapter("select * from
sanemp", con);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
sdr.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
public SqlDataReader Fetchdata(string ss)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from sanemp";
SqlDataReader sr;
return
sr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lbl = (Label)GridView1.Rows[e.RowIndex].FindControl("Empid");
int myid = Convert.ToInt32(lbl.Text);
TextBox txt1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
DropDownList ddl12 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlcity");
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(new SqlParameter("@EmpId", SqlDbType.Int)).Value = myid;
cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 50)).Value
= txt1.Text;
cmd.Parameters.Add(new SqlParameter("@City", SqlDbType.VarChar, 50)).Value
= ddl12.SelectedItem.Value.ToString();
con.Open();
cmd = new SqlCommand("Update sanemp set Name ='" + txt1.Text + "'
, City ='" + ddl12.SelectedItem.Value +
"' where EmpId ='" + myid + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
bindbind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindbind();
}
}