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.
Using IF ELSE Condition with EVAL function in ASP.Net GridView
The following HTML Markup consists of an ASP.Net GridView consisting of two BoundField columns and one TemplateField column populated using EVAL function.
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="GridView1" 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="GridView1" 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", true, false) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
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");
GridView1.DataSource = dt;
GridView1.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")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Screenshot
Demo
Downloads