In this article I will explain with an example, how to show and hide Button in GridView based on condition in ASP.Net using C# and VB.Net.
The Visible property of the Button is set with an Inline Expression which compares the value of the column and based on the value, True or False value is returned which is used to show or hide the Button respectively.
 
 

HTML Markup

The HTML Markup consists of:

Using IF ELSE Condition with EVAL function in ASP.Net GridView

GridView – for displaying data.

Columns

The GridView consists of two BoundField columns and TemplateField.
TemplateField - The TemplateField column consists of ItemTemplate and Button.
The Visible property of the Button is set with an Inline Expression which compares the value of Status column and based on whether the value is A or P, True or False value is returned.
Using Inline Expression, the value of the EVAL function is compared and if the Status value is A then the Delete Button is displayed else the Button is hidden.
Note: The syntax of using EVAL function within Inline Expression is different for C# and VB.Net.
 
C#
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:Button ID="btnDelete" runat="server" Text="Delete" Visible='<%# Eval("Status").ToString() == "A"  ?true : false %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 
 
VB.Net
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:Button ID="btnDelete" runat="server" Text="Delete" Visible='<%# If(Eval("Status").ToString() = "A"truefalse)%>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 
 
Note: If the conditions are more complex, it is recommended to use RowDataBound event. For more details refer my article, Dynamically change GridView Cell value using RowDataBound event in ASP.Net using C# and VB.Net.
 
 

Namespaces

You will need to import the following namespace.
C#
using System.Data; 
 
VB.Net
Imports System.Data 
 
 

Binding the ASP.Net GridView control

Inside the Page_Load event handler, an object of DataTable is created.
Then, three columns are added to the DataTable Columns collection using the AddRange method.
An Array of objects of type DataColumn is specified which will hold the name and the optional parameter Data Type i.e. the Type of the column.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
Finally, DataTable is assigned to the DataSource property of the GridView and the GridView is populated.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] {
                            new DataColumn("Id"),
                            new DataColumn("Name"),
                            new DataColumn("Status") });
        dt.Rows.Add(1, "John Hammond", "A");
        dt.Rows.Add(2, "Mudassar Khan", "P");
        dt.Rows.Add(3, "Suzanne Mathews", "P");
        dt.Rows.Add(4, "Robert Schidner", "A");
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();
    }
} 
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {
                            New DataColumn("Id"),
                            New DataColumn("Name"),
                            New DataColumn("Status")})
        dt.Rows.Add(1, "John Hammond", "A")
        dt.Rows.Add(2, "Mudassar Khan", "P")
        dt.Rows.Add(3, "Suzanne Mathews", "P")
        dt.Rows.Add(4, "Robert Schidner", "A")
        gvCustomers.DataSource = dt
        gvCustomers.DataBind()
    End If
End Sub
 
 

Screenshot

Show Hide Button in GridView based on condition in ASP.Net
 
 

Demo

 
 

Downloads