protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGrid();
}
}
private
void
BindGrid()
{
List<employee> employees =
new
List<employee>()
{
new
Employee() { ID = 1, Name =
"John Doe"
, Age = 30 },
new
Employee() { ID = 2, Name =
"Jane Smith"
, Age = 25 },
new
Employee() { ID = 3, Name =
"Bob Johnson"
, Age = 40 }
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected
void
GridView1_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int
id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
TextBox txtName = (TextBox)row.FindControl(
"txtName"
);
string
name = txtName.Text;
Response.Write($
"ID: {id}, New Name: {name}"
);
GridView1.EditIndex = -1;
BindGrid();
}
protected
void
GridView1_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
{
int
id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
BindGrid();
}
public
class
Employee
{
public
int
ID {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
int
Age {
get
;
set
; }
}
</employee></employee>