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

ASP.NETC#

Avatar of undefined
Last Comment
AvinashKN

8/22/2022 - Mon
pvginkel

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

Open in new window

TimCottee

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
ASKER
AvinashKN

How do I specify the current row?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
pvginkel

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?
ASKER
AvinashKN

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

TimCottee

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
AvinashKN

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
pvginkel

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
AvinashKN

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.