1

I am trying to toggle whether or not a CommandField.ShowEditButton is true or false with the following code:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        clsEmployee employee = (clsEmployee)e.Row.DataItem;
        CommandField cmdField = (CommandField)((DataControlFieldCell)e.Row.Cells[0]).ContainingField;
        cmdField.ShowEditButton = _currentUser.FullName.Equals(employee.FullName);
    }
}

It works, but on the proceeding line; not the current one. If I am logged in as John Smith, this happens:

    +------+-------------------------------------+
    |      | John Smith                          |
    +------+-------------------------------------+
    | Edit | Jane Doe                            |
    +------+-------------------------------------+

I know I am on the right row, because this works (ShowEditButton is always true for the CommandField):

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        clsEmployee employee = (clsEmployee)e.Row.DataItem;
        e.Row.Cells[0].Enabled = _currentUser.FullName.Equals(employee.FullName);
    }
}

When I am logged in as John Smith, I get:

    +------+-------------------------------------+
    | Edit | John Smith                          |
    +------+-------------------------------------+
    | Edit | Jane Doe                            |
    +------+-------------------------------------+

with the John Smith Edit being enabled and the Jane Doe one being disabled.

How can I get the first method to work?

jhasell
  • 31
  • 4
  • Which line works, and which one does not? – 123 456 789 0 Aug 27 '13 at 18:15
  • Where I set the ShowEditButton does not work, or at least not on the correct row. Setting Enabled does work for the correct row. – jhasell Aug 27 '13 at 20:06
  • What is _currentUser and how is this being set? – 123 456 789 0 Aug 27 '13 at 20:20
  • And what is your objective here, do you want to disable the Row when ShowEditButton is false? – 123 456 789 0 Aug 27 '13 at 20:21
  • The gridview shows all employees. I only want the Edit button to show on the row that matches the logged in user. – jhasell Aug 27 '13 at 21:09
  • Can you not just set the DataTemplate of the Row for DataGrid that has the edit button in it and do a binding if the current user is that one, then show the edit button? – 123 456 789 0 Aug 27 '13 at 21:29
  • If I cannot resolve this any other way, I will have to do it like this. But I would prefer to find a way to ensure the Edit button is shown on the correct line without modifying the template. – jhasell Aug 28 '13 at 15:59
  • I don't see in the code where you are setting the IsEnabled of the Cell based on this cmdField.ShowEditButton = _currentUser.FullName.Equals(employee.FullName); – 123 456 789 0 Aug 28 '13 at 16:17
  • I have added examples to the original post to hopefully make the situation clearer. – jhasell Aug 29 '13 at 16:45
  • Have you debug it non what is the _currentUser and employee.FullName? Is the employee.FullName different on the second row, and how many times does this RowDataBound event gets called – 123 456 789 0 Aug 29 '13 at 20:34

0 Answers0