GridView -enable editing

G

Guest

How do you enable editing in a GridView programatically rather than via its
Tasks menu?

Guy
 
M

Michael Nemtsev, MVP

Hello guy,

try to set the AutoGenerateEditButton = true;

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


g> How do you enable editing in a GridView programatically rather than
g> via its Tasks menu?
g>
g> Guy
g>
 
G

Guest

Michael,
AutoGenerateEditButton is already true,
and ithe edit button appears, What do I need to do in the RowEditing event
to actually edit the data?

Guy
 
M

Michael Nemtsev, MVP

Hello guy,

then use GridView.EditIndex setting the row to edit

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


g> Michael,
g> AutoGenerateEditButton is already true,
g> and ithe edit button appears, What do I need to do in the RowEditing
g> event
g> to actually edit the data?
g> Guy
g>
"MVP" wrote: said:
Hello guy,

try to set the AutoGenerateEditButton = true;

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:
http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high
and we miss it, but that it is too low and we reach it" (c)
Michelangelo

g> How do you enable editing in a GridView programatically rather
than
g> via its Tasks menu?
g>
g> Guy
g>
 
E

Eliyahu Goldin

You need to handle 3 events. Here is an example for editing a grid with user
info. Note using EditIndex property.

protected void dgUsers_RowEditing(object sender, GridViewEditEventArgs e)

{

System.Web.UI.WebControls.GridView grid = sender as
System.Web.UI.WebControls.GridView;

grid.EditIndex = e.NewEditIndex;

grid.DataSource = System.Web.Security.Membership.GetAllUsers();

grid.DataBind();

}

protected void dgUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

System.Web.UI.WebControls.GridView grid = sender as
System.Web.UI.WebControls.GridView;

System.Web.Security.MembershipUser userToUpdate =
System.Web.Security.Membership.GetUser(dgUsers.DataKeys[e.RowIndex].Value.ToString());

userToUpdate.Email = (dgUsers.Rows[e.RowIndex].Cells[5].Controls[0] as
System.Web.UI.WebControls.TextBox).Text;

System.Web.Security.Membership.UpdateUser(userToUpdate);

bool isAdministrator =
(dgUsers.Rows[e.RowIndex].Cells[4].FindControl("chbAdministrator") as
System.Web.UI.WebControls.CheckBox).Checked;

if (isAdministrator)

System.Web.Security.Roles.AddUserToRole (userToUpdate.UserName, "Admin");

else

System.Web.Security.Roles.RemoveUserFromRole(userToUpdate.UserName,
"rAdmin");

grid.EditIndex = -1;

grid.DataSource = System.Web.Security.Membership.GetAllUsers();

grid.DataBind();

}


protected void dgUsers_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)

{

System.Web.UI.WebControls.GridView grid = sender as
System.Web.UI.WebControls.GridView;

grid.EditIndex = -1;

grid.DataSource = System.Web.Security.Membership.GetAllUsers();

grid.DataBind();

}



--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


guy said:
Michael,
AutoGenerateEditButton is already true,
and ithe edit button appears, What do I need to do in the RowEditing event
to actually edit the data?

Guy

Hello guy,

try to set the AutoGenerateEditButton = true;

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and
we
miss it, but that it is too low and we reach it" (c) Michelangelo


g> How do you enable editing in a GridView programatically rather than
g> via its Tasks menu?
g>
g> Guy
g>
 
G

Guest

Thanks Guys,
it was the resetting of the DataSource and re-binding that I was missing

cheers

Guy

Eliyahu Goldin said:
You need to handle 3 events. Here is an example for editing a grid with user
info. Note using EditIndex property.

protected void dgUsers_RowEditing(object sender, GridViewEditEventArgs e)

{

System.Web.UI.WebControls.GridView grid = sender as
System.Web.UI.WebControls.GridView;

grid.EditIndex = e.NewEditIndex;

grid.DataSource = System.Web.Security.Membership.GetAllUsers();

grid.DataBind();

}

protected void dgUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

System.Web.UI.WebControls.GridView grid = sender as
System.Web.UI.WebControls.GridView;

System.Web.Security.MembershipUser userToUpdate =
System.Web.Security.Membership.GetUser(dgUsers.DataKeys[e.RowIndex].Value.ToString());

userToUpdate.Email = (dgUsers.Rows[e.RowIndex].Cells[5].Controls[0] as
System.Web.UI.WebControls.TextBox).Text;

System.Web.Security.Membership.UpdateUser(userToUpdate);

bool isAdministrator =
(dgUsers.Rows[e.RowIndex].Cells[4].FindControl("chbAdministrator") as
System.Web.UI.WebControls.CheckBox).Checked;

if (isAdministrator)

System.Web.Security.Roles.AddUserToRole (userToUpdate.UserName, "Admin");

else

System.Web.Security.Roles.RemoveUserFromRole(userToUpdate.UserName,
"rAdmin");

grid.EditIndex = -1;

grid.DataSource = System.Web.Security.Membership.GetAllUsers();

grid.DataBind();

}


protected void dgUsers_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)

{

System.Web.UI.WebControls.GridView grid = sender as
System.Web.UI.WebControls.GridView;

grid.EditIndex = -1;

grid.DataSource = System.Web.Security.Membership.GetAllUsers();

grid.DataBind();

}



--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


guy said:
Michael,
AutoGenerateEditButton is already true,
and ithe edit button appears, What do I need to do in the RowEditing event
to actually edit the data?

Guy

Hello guy,

try to set the AutoGenerateEditButton = true;

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and
we
miss it, but that it is too low and we reach it" (c) Michelangelo


g> How do you enable editing in a GridView programatically rather than
g> via its Tasks menu?
g>
g> Guy
g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top