<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview.aspx.cs" Inherits="gridview" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id"
onrowcancelingedit="gv_RowCancelingEdit" onrowdeleting="gv_RowDeleting"
onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="productname">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("productname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("productname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productprice">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("productprice") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("productprice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="insert" />
<asp:TextBox ID="TextBox4" runat="server" Width="73px"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server" Width="73px"></asp:TextBox>
</form>
</body>
</html>
Code
Insert update and delete in gridview
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;
public partial class gridview : System.Web.UI.Page
{
string constr = "data source=ravi;database=ravi;integrated security=yes";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();
}
}
private void binddata()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(constr))
{
SqlDataAdapter adp = new SqlDataAdapter("select *from service", con);
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
gv.DataSource = dt;
gv.DataBind();
}
}
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
binddata();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id=int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
deleteservice(id);
binddata();
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
TextBox textbox2 = (TextBox)gv.Rows[e.RowIndex].FindControl("textbox2");
TextBox textbox3 = (TextBox)gv.Rows[e.RowIndex].FindControl("textbox3");
updateservice(id,textbox2.Text,int.Parse(textbox3.Text));
gv.EditIndex=-1;
binddata();
}
private void updateservice(int id,string productname,int productprice)
{
using (SqlConnection con = new SqlConnection(constr))
{
string query="update service set productname='"+productname+"',productprice="+productprice+" where id="+id+"";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
}
}
private void deleteservice(int id)
{
using (SqlConnection con = new SqlConnection(constr))
{
string query = "delete from service where id=" + id + "";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string productname = TextBox4.Text;
decimal productprice =decimal.Parse(TextBox5.Text);
using (SqlConnection con = new SqlConnection(constr))
{
string query="insert into service values('"+TextBox4.Text+"',"+TextBox5.Text+")";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
gv.DataBind();
con.Close();
binddata();
}
}
}