Link to home
Start Free TrialLog in
Avatar of rcowen00
rcowen00Flag for United States of America

asked on

Gridview Hide Delete on Certain Records but show empty column

I have the following and it works, but it removes the column when there is no Delete link.  I would like the column to show, but be empty when there is no delete link.


User generated image
 Protected Sub txtAddendum_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles txtAddendum.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim drv As DataRowView = TryCast(e.Row.DataItem, DataRowView)
            If drv("OrdersAddendum").ToString().Equals("0") Then
                e.Row.Cells(0).Visible = False
            Else
                e.Row.Cells(0).Visible = True
            End If
        End If
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of psgindiana
psgindiana

instead of
e.Row.Cells(0).Visible = False

Open in new window

use:
            foreach (Control c in e.Row.Cells(0).Controls)
            {
                 c.Visible = False;
            }

Open in new window

which should keep the table structure but hide the controls inside the cell.

** this is c#. I don't know VB enough not to jack up some syntax. You should be able to figure out how it's working, though, and fix it accordingly ("Dim c As Control" instead of "Control c", etc.)
I use a TemplateField for setting visibility like:

<asp:TemplateField HeaderText="Delete" HeaderStyle-Width="30px" HeaderStyle>
                <ItemTemplate>
                    <asp:LinkButton ID="DeleteButton" runat="server" CommandName= "Delete"   Visible='<%#IsVisible(Eval("IsPending"))%>' />
                   </ItemTemplate>
</asp:TemplateField>

Open in new window

In Code-behind IsVisible method looks like this:

Public Function IsVisible(ispending As Object) As Boolean
	Return Not CBool(ispending)
        'You can use any logic here to determine whether to return true or false
End Function

Open in new window