In Default2.aspx.cs
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
Class1 ob = new Class1();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
show();
}
public void show()
{
GridView1.DataSource = ob.selec();
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
show();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gr=GridView1.Rows[e.RowIndex];
string id = ((TextBox)gr.Cells[0].Controls[0]).Text;
string na = ((TextBox)gr.Cells[1].Controls[0]).Text;
string age = ((TextBox)gr.Cells[2].Controls[0]).Text;
ob.up(na, Convert.ToInt32(age), Convert.ToInt32(id));
GridView1.EditIndex = -1;
show();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
show();
}
//Get Values in Row Deleting Event
//string Val=GridView1.Rows[e.RowIndex].Cells[1].Text;
}
In Class1.cs
using System.Data.SqlClient;
///
/// Summary description for Class1
///
public class Class1
{
SqlConnection con = new SqlConnection(@"Data Source=INDIA\SQLEXPRESS;Initial catalog=ani;integrated security=true");
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public DataTable selec()
{
SqlDataAdapter da = new SqlDataAdapter("select * from aa", con);
DataSet ds = new DataSet();
da.Fill(ds,"gg");
return ds.Tables["gg"];
}
public void up(string name, int age,int id)
{
SqlCommand cmd = new SqlCommand("update aa set name='" + name + "',age=" + age + " where id=" + id + "", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
ImageButton1_Click on Gridview
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow grdRow = (GridViewRow)imgbtn.NamingContainer;
LinkButton name = (LinkButton)imgbtn.FindControl("controlIdName");
Label tId = (Label)imgbtn.FindControl("Label1");
string ab = tId.Text;
}
Editing GridView Row Using Button1
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gr = (GridViewRow)btn.NamingContainer;
GridView1.EditIndex = gr.RowIndex;
show();
}