Hi, this can be done in many ways, either by using css or server side code. here i have given 3 different possiblities.
-- aspx code --
<asp:GridView ID="UsersGv" runat="server" AutoGenerateColumns="false" OnRowDataBound="UsersGvRowDataBound" ShowFooter="true" ShowHeader="true" AllowPaging="true"
RowStyle-Font-Names="arial" HeaderStyle-BackColor="Gray" HeaderStyle-Font-Names="arial"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="LinkAssignButton" runat="server" CommandName="Assign" Text="Take"
Visible="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="FirstNameGvLbl" runat="server" Text='<% #Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle Name">
<ItemTemplate>
<asp:Label ID="MiddleNameGvLbl" runat="server" Text='<% #Eval("MiddleName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="LastNameGvLbl" runat="server" Text='<% #Eval("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
--- .cs code ---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
/// <summary>
/// This propery is holds the number of rows exists in the datasource, which is used to bind the grid.
/// </summary>
private int NoOfRowsInGridDataSource
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUsers();
}
}
/// <summary>
/// UsersGvRowDataBound - This event is fired when the GridView Row data bounds.
/// Add a logic to hide the checkbox if the NoOfRowsInGridDataSource value is greater than 1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UsersGvRowDataBound(object sender, GridViewRowEventArgs e)
{
if (NoOfRowsInGridDataSource > 1)
{
// This will hide the entire check box column, using css
// UnComment the below line and comment rest of the code -- Solution 1
//e.Row.Cells[0].Attributes.Add("style", "display:none");
// This will hide the entire check box column, this is serer side code
// UnComment the below line and comment rest of the code -- Solution 2
//e.Row.Cells[0].Visible = false;
// This will check box, but does not hide the column, so the check box is hidden but still you can see the column with header and footer etc., this is serer side code
// This is -- Solution 3
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox LinkAssignButton = (CheckBox)e.Row.Cells[0].FindControl("LinkAssignButton");
if (LinkAssignButton != null)
{
//Serverside code to hide the checkbox.
LinkAssignButton.Visible = false;
//css to hide the checkbox.
LinkAssignButton.Attributes.Add("style", "display:none");
}
}
}
}
private void BindUsers()
{
DataTable users = new DataTable();
users.Columns.Add("FirstName");
users.Columns.Add("MiddleName");
users.Columns.Add("LastName");
users.Rows.Add(users.NewRow());
users.Rows[users.Rows.Count - 1]["FirstName"] = "First Name 1";
users.Rows[users.Rows.Count - 1]["MiddleName"] = "Middle Name 1";
users.Rows[users.Rows.Count - 1]["LastName"] = "Last Name 1";
users.Rows.Add(users.NewRow());
users.Rows[users.Rows.Count - 1]["FirstName"] = "First Name 2";
users.Rows[users.Rows.Count - 1]["MiddleName"] = "Middle Name 2";
users.Rows[users.Rows.Count - 1]["LastName"] = "Last Name 2";
//Before binding the grid, set the value of NoOfRowsInGridDataSource property with the number of rows or count of the records which are in the grid view datasource.
NoOfRowsInGridDataSource = users.Rows.Count;
UsersGv.DataSource = users;
UsersGv.DataBind();
}
}
Hope the above code helps you, let me know if you need anything else or if you have any questions.