Hi makumbisulaim...,
You can't hide the GridView Header cell based on the condition.
You need to check the specific word in each GridView header cell and if condition satisfied then set the Cell Text property with empty string.
Refer below example.
HTML
<asp:GridView ID="gvDetails" runat="server" OnRowDataBound="OnRowDataBound"></asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
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 Set"),
new DataColumn("Name"),
new DataColumn("Country Set")
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Text.ToLower().Contains("set"))
{
cell.Text = string.Empty;
}
}
}
}
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 Set"),
New DataColumn("Name"),
New DataColumn("Country Set")
})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvDetails.DataSource = dt
gvDetails.DataBind()
End If
End Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
For Each cell As TableCell In e.Row.Cells
If cell.Text.ToLower().Contains("set") Then
cell.Text = String.Empty
End If
Next
End If
End Sub
Screenshot
Note: Here i am making use of Contains function. You can change it with equal or with other methods.