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