Setting Button property in a Gridview TemplateField ONLY if there is a single row

I'm using .Net 2.0 and coding in I have a Gridview which displays an edit and a delete button for each record. I cannot allow the user to delete the row if it is the only row in the grid so I would like to set the visible property of my delete button to False if it is the only row.

Currently I am   checking Grid.Rows.Count = 1 in my Grid.RowCommand event after the grid has been created and displaying a message but I would like to simply hide the delete button.

I have tried the solution below in both the RowDataBound and DataBound without success. When I write the Count back to the browser it is incrementing so that my code will execute incorrectly if there are more than 1 rows.  Also the query "If e.Row.RowType = DataControlRowType.DataRow " never fires so even if the next code bit worked it wouldn't fire:
                Dim DelButton As Button = CType(e.Row.FindControl("btnDelete"), Button)
                DelButton.Visible = False
or, equally unsuccessful,
                e.Row.FindControl("btnDelete").Visible = False

Here is the aspx part of my code:
                 <asp:Button ID="btnDelete" runat="server" CommandName="RemoveDetail" CausesValidation="False" Text="Del"  />

I should also mention that I am using Master Pages and this code is on the content page.
Could someone get me moving in the right direction on this?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thefarmAuthor Commented:
I also have tried setting a variable to False or True in the grid_PreRender event and then using

<asp:Button Visible = "<%# varSetInPrerender %>" ID="btnDelete" />

in my aspx code but that isn't doing the trick either although I am able to get a good grid..Rows.Count in PreRender.

After a GridView is completely bound, then you can access Grid.Row.Count. But RowDataBound fires before the GridView is completly bound, so if you try to call Grid.Row.Count in this event handler, it will return 0. The same as DataBound event handler.
However, RowCommand fires after the GridView is bound, so you can use GridView.Row.Count to get the number of the rows (if you don't have paging). If you have paging enabled, GridView.Rows.Count returns the number of rows for the current page, and it is NOT the actual total number rows of your GridView. Be careful here.
I would suggest you to get the actual total number of records from your datasource, then save it in ViewState, then in your RowDataBound or RowCreated (I prefer to use this one), you can check the ViewState, then hide/display the delete button.
thefarmAuthor Commented:
By putting the rowcount into the viewstate from my datasource I am able to consistently identify the rowcount value. However I am still unable to find the button using either:

Dim DelButton As Button = CType(e.Row.FindControl("btnDelete"), Button)
DelButton.Visible = False
e.Row.FindControl("btnDelete").Visible = False

both of these return a "Object reference not set to an instance of an object". I am using these in the OnRowCreated as PrairieDog suggested.

I believe this is because I am using Master Pages  and my routines are happening on a content page? Also the Button is in an ItemTemplate tag..not sure if that effects the discovery of the control. I'd like to understand this better so if there are any references that are good I'd appreciate knowing them.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Are you saying that your GridView is on the master page, and you are accessing it from the content page?
thefarmAuthor Commented:
No my Gridview is on the content page and I'm using  the codebehind to manipulate it.

I actually got it to work by doing this in the RowCreated Method:

        If ViewState("CurrentRowCount") = 1 Then
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim DelButton As Button = CType(e.Row.FindControl("btnDelete"), Button)
                DelButton.Visible = False
            End If
        End If

I had to check first if I was on a DataRow. It works perfectly now. I guess the master page wasn't the problem afterall. It was just bombing on the header row maybe?
Yes, you are right. No matter what event handler you are using, RowDataBoudn or RowCreated, you will always need to check if it is a DataRow. But for RowCommand event handler, you don't need to check because only a DataRow can fire a command.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.