Hey guys,
This blog we
discuss how to update Gridview data using asp.net.
First create a like table name here is sanemp
EmpId, Name,City
Gridgrid.aspx page code:-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<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>
</asp:GridView>
Code behind page page :-
Gridgrid.aspx.cs file code
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();
}
}