Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am taking checkbox in gridview this wat and other data is coming from datbase
i want if there is more than one row then checkbox should not be visible
how to do that
<asp:TemplateField>
<itemtemplate>
<asp:CheckBox ID="LinkAssignButton" runat="server"

CommandName="Assign" 

Text="Take" Visible="true">

--%>

</itemtemplate>


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 11-May-11 21:47pm
v2
Comments
Ashishmau 12-May-11 3:43am    
Improve ur question
Richard MacCutchan 12-May-11 4:44am    
You too, don't use 'txtspk'.

1 solution

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 --
XML
<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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900