Hide GridView Header and Rows based on Label value using C# in ASP.Net

RichardSa
 
on Dec 05, 2022 03:56 AM
447 Views

How can I hide GridView Header and Row based on the value of asp Label control.

I was trying to hide and show GridView Header and Row based on the value of a Label but it is not working.

I want it that when the value of the label is "Admin" the column head and row is hidden but when the label value is "user" It will show.

Please can anybody help me?

protected void DataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[4].Visible = false;
        e.Row.Cells[5].Visible = false;
        Label Role = e.Row.FindControl("UserRole") as Label;
        CheckBox checker1 = (e.Row.FindControl("checker1") as CheckBox);
        cmd.CommandText = "select * from Users where Uid= '" + Session["user"] + "'";
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(ds, "detail");
        if (ds.Tables[0].Rows.Count > 0)
        {
          //  string Role;
            Role = ds.Tables[0].Rows[0][3].ToString.Trim();
 
            if (Role == "User")
            {
                checker1.Visible = false;
            }
            else if (Role == "Admin")
            {
                checker1.Visible = true;
            }
        }
    }
 
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 1; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
            e.Row.Cells[i].Style.Add("cursor", "pointer");
            e.Row.Cells[i].ToolTip = "View Details.";
        }
        e.Row.Cells[4].Visible = false;
        CheckBox checker1 = (e.Row.FindControl("checker1") as CheckBox);
        cmd.CommandText = "select * from Users where Uid= '" + Session["user"] + "'";
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(ds, "detail");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string Role;
            Role = ds.Tables[0].Rows[0][3].ToString().Trim();
 
            if (Role == "User")
            {
                checker1.Visible = false;
            }
            else if (Role == "Admin")
            {
                checker1.Visible = false;
            }
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
PrinceG
 
on Dec 06, 2022 02:40 AM
on Dec 06, 2022 02:47 AM

Hi RichardSa,

Here, We set label value as User and in condition we set the value as admin then the checkbox is visible and if value is not equal to admin then the checkbox is not visible.  

Please refer below sample.

SQL

Create table [WebUsers]
(
	[Receipt_No] INT IDENTITY (1,1),
	[Rname] VARCHAR(40),
	[Email] VARCHAR(50),
	[Role] VARCHAR(50)
)

INSERT INTO [WebUsers] VALUES ('John Hammond','John@Gmail.com','User')
INSERT INTO [WebUsers] VALUES ('Mudassar Khan','Mudassar@Gmail.com','Admin')
INSERT INTO [WebUsers] VALUES ('Suzanne Mathews','Suzanne@Gmail.com','User')
INSERT INTO [WebUsers] VALUES ('Robert Schidner','Robert@Gmail.com','Admin')

SELECT * FROM [WebUsers]
DROP TABLE [WebUsers]

HTML

<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="panel" runat="server">
    <ContentTemplate>
        <asp:Label runat="server" ID="lblRole" Text="User"></asp:Label>
        <asp:GridView ID="GridView1" runat="server" GridLines="None" DataKeyNames="receipt_no" AllowPaging="true" 
            HeaderStyle-ForeColor="#05214d" HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="11pt" Font-Size="10pt"
            AutoGenerateColumns="false" class="table" Width="100%">
            <EmptyDataTemplate>
                <div style="text-align: center; font-weight: 500; font-size: medium;">
                    <hr />
                    <hr />
                    <asp:Label ID="labelTemp" runat="server" Font-Size="12pt" Text="No Record"></asp:Label>
                    <hr />
                    <hr />
                </div>
            </EmptyDataTemplate>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="checker1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="receipt_no" HeaderText="ID" HeaderStyle-Font-Bold="false" />
                <asp:BoundField DataField="rname" HeaderText="rname" HeaderStyle-Font-Bold="false" />
                <asp:BoundField DataField="email" HeaderText="email" HeaderStyle-Font-Bold="false" />
                <asp:BoundField DataField="Role" HeaderText="User_Role" HeaderStyle-Font-Bold="false" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

Namespaces

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

Code

private int PageSize = 5;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        showdata2();
    }
}

public void showdata2()
{
    string constring = @"Data Source=.\SQL2019;DataBase=Test;UID=sa;PWD=PassWord";
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("Select * from WebUsers", con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds, "detail");
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
            }
        }
    }

    if (lblRole.Text.Trim().ToLower() == "admin")
    {
        GridView1.Columns[0].Visible = false;
    }
}

Screenshot