Hide GridView Column based on Label value using C# and VB.Net in ASP.Net

akhter
 
on Dec 02, 2021 12:20 AM
Sample_193864.zip
351 Views

i am trying to hide column base on condition, but it is not hiding

protected void GVballist_RowDataBound(object sender, GridViewRowEventArgs e)
{    
    const int invno = 3;
    if (lbempid.Text == "3" )
    {
        // Hide the Countries column
        this.GVballist.Columns[invno].Visible = true;
    }
    else
    {
        this.GVballist.Columns[invno].Visible = false;
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Dec 02, 2021 04:32 AM

Hi Akhter,

 Please refer below Sample.

Database

I have made use of the following table Customers with the schema as follows.

I have already inserted few records in the table.

You can download the database table SQL by clicking the download link below.

Download SQL file 

HTML

<table>
    <tr>
        <td>ID:</td>
        <td><asp:Label ID="lblEmpId" runat="server" Text="3" /></td>
    </tr>
</table>
<br />
<br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false" OnRowDataBound="RowDataBound">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="ID" />
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Name") %>' ID="lblName" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Country") %>' ID="lblCountry" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

 VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}

private void BindGrid()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    gvCustomers.DataSource = dt;
                    gvCustomers.DataBind();
                }
            }
        }
    }
}

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (lblEmpId.Text.Trim() == "3")
        {
            gvCustomers.Columns[2].Visible = false;
        }
        else
        {
            gvCustomers.Columns[2].Visible = true;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub

Private Sub BindGrid()
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    gvCustomers.DataSource = dt
                    gvCustomers.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub

Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then

        If lblEmpId.Text.Trim() = "3" Then
            gvCustomers.Columns(2).Visible = False
        Else
            gvCustomers.Columns(2).Visible = True
        End If
    End If
End Sub

Screenshot