I have a gridView with 5 columns with Edit and Delete button on each row of GridView. Once an Edit button a particular row is clicked , I want to disable the edit and delete
You could use javascript, outlined here, http://www.krissteele.net/blogdetails.aspx?id=92 . Add logic to not disable for the current row.
The approach you are taking will work fine. However the CommandArgument which is set in your markup CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
is not a reference to the Button itsself but the index of the GridView row the Button resides in.
You will need to retrieve the index from the CommandArgument in the RowCommand event and then enable/disable all buttons on other rows like so:
void gridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "edit":
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
foreach (GridViewRow row in gridView1.Rows)
{
if (row.RowIndex != rowIndex)
{
Button editButton = (Button)row.FindControl("btnEdit");
editButton.Enabled = false;
Button deleteButton = (Button)row.FindControl("btnDelete");
deleteButton.Enabled = false;
}
}
}break;
}
}
Edit (based on comment)
Try moving the code that modifies the delete button to the RowEditing event handler like so:
void gridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
gridView1.EditIndex = e.NewEditIndex;
DataBind();
Button deleteButton = (Button)gridView1.Rows[e.NewEditIndex].FindControl("btnDelete");
deleteButton.Text = "Cancel";
deleteButton.CommandName = "Cancel";
}
void gridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridView1.EditIndex = -1;
DataBind();
}
Hope this helps.