Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have bounded the values in the grid view using html table. Each row contains html table and one grid view row displayed per page. Now I wanted to hide some cells(checkbox 1,2,3,4) from second row (second record) onwards. It's huge code to paste here and I added some sample code.. Please help me out...
<asp:GridView ID="grdSampleForm" runat="server" AutoGenerateColumns="False" 
AllowPaging="True" AllowSorting="True" DataKeyNames="SampleID" DataSourceID="SqlDataSourceSampleFormDetails" PageSize="1" 
ForeColor="#333333" GridLines="None" CssClass="smallText" OnRowDataBound="grdSampleForm_RowDataBound" Width="100%"  OnRowUpdating="grdSampleForm_RowUpdating" OnRowCreated="grdSampleForm_RowCreated" >

     <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                <PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

        <asp:TemplateField HeaderText="SampleForm Details">

                <table style="font-size:x-small;" border="0" cellpadding="1" cellspacing="1">
                        <td style="font-size:small;" colspan="2">Sample Date : <asp:Label ID="lblSampleDate" Text='<%# Eval("SampleDate", "{0:dd MMM yyyy}") %>' runat="server" /></td> 

                        <td><asp:CheckBox ID="CheckBox1" runat="server" Text="Sample1" Checked= '<%# Eval("Sample1") %>' Enabled="false"  TextAlign="Left" /></td>
                        <td><asp:CheckBox ID="CheckBox2" runat="server" Text="Sample2" Checked= '<%# Eval("Sample2") %>' Enabled="false"  TextAlign="Left" /></td>
                        <td><asp:CheckBox ID="CheckBox3" runat="server" Text="Sample3" Checked= '<%# Eval("Sample3") %>' Enabled="false"  TextAlign="Left" /></td>
                        <td><asp:CheckBox ID="CheckBox4" runat="server" Text="Sample4" Checked= '<%# Eval("Sample4") %>' Enabled="false"  TextAlign="Left" /></td>
                        <td colspan="2"><asp:CheckBox ID="CheckBox5" runat="server" Text="Sample5" Checked= '<%# Eval("Sample5") %>' Enabled="false" TextAlign="Left" /></td>
                        <td colspan="2">Sample Type : <asp:Label ID="lblSampleType" Text='<%# Eval("SampleTypeDesc")%>' runat="server" /></td>

                        <td><asp:CheckBox ID="CheckBox6" runat="server" Text="Sample6" Checked= '<%# Eval("Sample6") %>' Enabled="false"  TextAlign="Left" /></td>
                        <td><asp:CheckBox ID="CheckBox7" runat="server" Text="Sample7" Checked= '<%# Eval("Sample7") %>' Enabled="false"  TextAlign="Left" /></td>


                <table style="font-size:small;" border="0" cellpadding="1" cellspacing="1">


              <asp:CommandField ShowEditButton="True" ButtonType="Image" ShowHeader="true" EditImageUrl="~/Images/database_edit.png" UpdateImageUrl="~/Images/database_save.png" CancelImageUrl="~/Images/cancel.png" ItemStyle-Width="5%" EditText="Edit">
              <ItemStyle  VerticalAlign="Bottom" />


    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
Updated 27-Nov-15 1:32am
So where you are trying to hide? In JavaScript or code behind? If yes, then which event?
Member 9018012 27-Nov-15 7:30am    
I used c# code behind rowbound event and tried to find the row index but no luck..

1 solution

I used c# code behind rowbound event and tried to find the row index but no luck..
You have to find the control inside the event so that you can hide because you are using TemplateFields.
var checkbox = e.Row.FindControl("CheckBoxID") as CheckBox;
checkbox.Visible = false;
Share this answer
Richard Deeming 27-Nov-15 10:31am    
The as operator can return null if the input is null or not of the specified type. You should check whether the checkbox variable is null before trying to set its Visible property. :)
Member 9018012 27-Nov-15 11:52am    
protected void grdSampleForm_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)

if(e.Row.RowIndex > 1)

CheckBox CheckBox1 = (CheckBox)e.Row.FindControl("CheckBox1");
CheckBox1.Visible = false;
CheckBox CheckBox2 = (CheckBox)e.Row.FindControl("CheckBox2");
CheckBox2.Visible = false;
// e.Row.Cells[0].Controls[2].Visible = false;
Member 9018012 27-Nov-15 11:52am    
but the above code does not skip the first row...
Debug and see what is the issue.
Perfect. Virtual 5 Richard. :)

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