.aspx page
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager2" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updpnl" runat="server">
<ContentTemplate>
<asp:GridView ID="grddata" runat="server" AutoGenerateColumns="false" OnRowUpdating="grddata_RowUpdating"
OnRowEditing="grddata_RowEditing" OnRowDeleting="grddata_RowDeleting" OnRowCancelingEdit="grddata_RowCancelingEdit">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblid" Width="200px" runat="server" Text="Id"></asp:Label>
</td>
<td>
<asp:Label ID="lblname" Width="200px" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:Label ID="lblsalary" runat="server" Width="200px" Text="Salary"></asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblid" Width="200px" Text= runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="lblname" Width="200px" Text='<%# Eval("name") %>' runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="lblsalary" Width="200px" runat="server" Text='<%# Eval("salary") %>'></asp:Label>
</td>
<td>
<asp:LinkButton ID="lnkedit" CommandName="Edit" OnClientClick="About.aspx.target='_blank';"
Text="Edit" CommandArgument='<%# Eval("id") %>' runat="server"></asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lnkdelete" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("id") %>'
runat="server"></asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="redirect" runat="server" Text="Redirect" OnClientClick="About.aspx" target="_blank"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:Label ID="lblid" Width="200px" Text='<%# Eval("id") %>' runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtsalary" runat="server"></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lnkupdate" CommandName="Update" Text="Update" CommandArgument='<%# Eval("id") %>'
runat="server"></asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lnkCancel" CommandName="Cancel" Text="Cancel" CommandArgument='<%# Eval("id") %>'
runat="server"></asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Data Available
</EmptyDataTemplate>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
.cs page
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"server=ADMIN-PC\NEERAJ;database=master;integrated security=true;");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
showdata();
}
}
//private void PDF_Export()
//{
// Response.ContentType = "application/pdf";
// Response.AddHeader("content-disposition",
// "attachment;filename=GridViewExport.pdf");
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
// StringWriter sw = new StringWriter();
// HtmlTextWriter hw = new HtmlTextWriter(sw);
// GridView1.AllowPaging = false;
// GridView1.DataBind();
// GridView1.RenderControl(hw);
// StringReader sr = new StringReader(sw.ToString());
// Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
// HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
// PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
// pdfDoc.Open();
// htmlparser.Parse(sr);
// pdfDoc.Close();
// Response.Write(pdfDoc);
// Response.End();
//}
public void showdata()
{
SqlDataAdapter da=new SqlDataAdapter("select * from empdata",con);
DataSet ds=new DataSet();
da.Fill(ds);
grddata.DataSource=ds;
grddata.DataBind();
}
protected void grddata_RowEditing(object sender, GridViewEditEventArgs e)
{
//grddata.EditIndex = -1;
//Label name = (Label)grddata.Rows[0].Cells[1].FindControl("lblname");
//TextBox salary = (TextBox)grddata.Rows[0].Cells[2].FindControl("txtsalary");
grddata.EditIndex = e.NewEditIndex;
//LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit");
// //Response.Redirect("About.aspx?name=neeraj&salary=1200");
//string url = "About.aspx?name=neeraj&salary=1200";
//edit.OnClientClick = "window.open('" + url + "'); return false;";
}
protected void grddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit");
Label name = (Label)grddata.Rows[e.RowIndex].FindControl("lblname");
//Label salary = (Label)grddata.Rows[0].FindControl("lblsal");
Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid");
ViewState["id"] = id.Text;
//string id = grddata.DataKeys
// [e.Item.ItemIndex].ToString();
//SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con);
SqlCommand cmd;
cmd = new SqlCommand("delete from empdata where id="+ViewState["id"]+" ", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
showdata();
con.Close();
}
protected void grddata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grddata.EditIndex = -1;
showdata();
}
protected void grddata_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//string id = grddata.DataKeys[e.Item.ItemIndex].ToString();
TextBox name = (TextBox)grddata.Rows[0].FindControl("txtname");
TextBox salary = (TextBox)grddata.Rows[0].FindControl("txtsalary");
//Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid.text");
//SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con);
SqlCommand cmd;
cmd = new SqlCommand("UPDATE empdata SET name='" + name.Text + "',salary=" + salary.Text + " ", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
grddata.EditIndex = -1;
showdata();
}
public void sendmail()
{
StringBuilder sb = new StringBuilder();
}
}