Link to home
Start Free TrialLog in
Avatar of AvinashKN
AvinashKN

asked on

Disabling delete button while editing Gridview

Hello all,

I would like to know if its possible to disable the delete button while the user is editing a row in a gridview. I've posted the code I was experiementing with but it obviously is not the right solution since it only disables the first row.
Button btn = (Button)dgUserLevel.Rows[0].FindControl("btnDelete");
            btn.Enabled = false;

Open in new window

Avatar of pvginkel
pvginkel
Flag of Netherlands image

Couldn't you replace the 0 with the row index?
dgUserLevel.SelectedRows[0].FindControl("btnDelete");

Open in new window

Hello AvinashKN,

Because that is what you have told it to do: (Button)dgUserLevel.Rows[0] <- First row.

Depending on what event you are using this in you have various properties such as e.RowIndex or dgUserLevel.EditIndex which will allow you to specify the current row.

Regards,

TimCottee
Avatar of AvinashKN
AvinashKN

ASKER

How do I specify the current row?
The SelectedRows property contains a collection of the currently selected rows. Since the user is editing, this will be one row. So, the following row is the current row:

dbUserLevel.Rows[0]

This should always work. Could you try this please?
I cannot specify rowindex either for whatever reason.
    protected void dgUserLevel_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            dgUserLevel.EditIndex = e.NewEditIndex;
            PopulateUserLevelDataGrid();
            Button btn = (Button) dgUserLevel.Rows[0].FindControl("btnDelete");
            btn.Enabled = false

Open in new window

AvinashKN,

Try this:

TimCottee
      protected void dgUserLevel_RowEditing(object sender, GridViewEditEventArgs e)
      {
          try
          {
              dgUserLevel.EditIndex = e.NewEditIndex;
              PopulateUserLevelDataGrid();
              GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
              Button btn = (Button) row.FindControl("btnDelete");
              btn.Enabled = false

Open in new window

Thanks for the help.

TimCottee:

I'm getting the following error.

'System.Web.UI.WebControls.GridViewEditEventArgs' does not contain a definition for 'CommandSource'

pvginkel:

I'm getting the following error:

Cannot apply indexing with [] to an expression of type 'System.Web.UI.WebControls.GridViewRow'
ASKER CERTIFIED SOLUTION
Avatar of pvginkel
pvginkel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help pvginkel. It works.

I would also like to thank Tim Cotee. Since I'm a complete newbie to asp.net programming, his explanation was very helpful.

Thank you both.