Simon Sauro
Simon Sauro

Reputation: 3

asp.net checkbox in gridview Why my checkbox is not showing up

<asp:GridView ID="grdInvPending" runat="server" AutoGenerateColumns="false" 
     CssClass="table table-hover" AllowSorting="true">
<Columns>
    <asp:BoundField DataField="LeagueName" HeaderStyle-BackColor="LightCoral"
         HeaderStyle-ForeColor="Black" ItemStyle-Width="250px" SortExpression="name" />                                                   
    <asp:TemplateField HeaderStyle-BackColor="LightCoral" HeaderStyle-ForeColor="Black">
        <ItemTemplate>
            <asp:CheckBox ID="chkAccepter" Visible="true" runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderStyle-BackColor="LightCoral" HeaderStyle-ForeColor="Black">
        <ItemTemplate>
            <asp:CheckBox ID="chkRefuser" Visible="true" runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

Upvotes: 0

Views: 1287

Answers (2)

Xami Yen
Xami Yen

Reputation: 267

Add "Text" property to your checkbox. If you don't want any text to show next to your checkbox, then use Text="&nbsp;". That should do the trick. Here is your full markup:

<asp:TemplateField>
    <HeaderTemplate>
        <asp:CheckBox ID="SelectAll" runat="server" Text="&nbsp;" />
    </HeaderTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="SelectOne" runat="server" Text="&nbsp;" />
    </ItemTemplate>
</asp:TemplateField>

Microsoft document on how to add checkboxes in GridView has good information that you might find useful. Here is the link https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/enhancing-the-gridview/adding-a-gridview-column-of-checkboxes-cs

Upvotes: 0

Albert D. Kallal
Albert D. Kallal

Reputation: 48989

You have to wrap the whole mess in the columns tag, like this:

<asp:GridView ID="GridView1" runat="server">
   <Columns>
      <asp:TemplateField HeaderStyle-BackColor="LightCoral" HeaderStyle-ForeColor="Black">
         <ItemTemplate>
             <asp:CheckBox ID="chkAccepter" Visible="true" runat="server" />
         </ItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderStyle-BackColor="LightCoral" HeaderStyle-ForeColor="Black">
         <ItemTemplate>
            <asp:CheckBox ID="chkRefuser" Visible="true" runat="server" />
         </ItemTemplate>
       </asp:TemplateField>
  </Columns>

</asp:GridView>

While I really like grid view? I think the "instant" you start customizing it - and adding custom controls? I then jump over and almost ALWAYS now use the listview.

Now the listview "seems" to have a truckload more markup, but I often generate my columns by using the wizard (setup a data source). I then blow out (delete) the edit temple, alternating, temple, and a few more.

The end result? Well, you get nice grid but NOW you can freely drag + drop controls from the toolbox right into the markup.

Each new control you add? It does NOT need the TemplateField->ItemTempalte (that's two extra open + close markup tags. So the listview "seems" a bit more to chew on, but in the long run, I find it MUCH nicer.

So, here is a listview as a grid:

    <link href="Content/cuscosky.css" rel="stylesheet" />
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" >
<ItemTemplate>
  <tr id="myTable1" style="">
    
     <td><asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' /></td>
     <td><asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Eval("Description") %>' /></td>
     <td><asp:TextBox ID="tQty" runat="server" Text='<%# Eval("Qty") %>' style="width:40px" /></td>
     <td><asp:TextBox ID="tUnitPrice" runat="server" Text='<%# Eval("UnitPrice") %>' style="width:70px;text-align:right" /></td>
     <td><asp:TextBox ID="tCost" runat="server" Text='<%# Eval("Qty") * Eval("UnitPrice") %>' style="width:70px;text-align:right"  /></td>
            
    </tr>
 </ItemTemplate>
 <LayoutTemplate>
   <table id="itemPlaceholderContainer" runat="server" border="0" >
     <tr runat="server" style="">
     <th runat="server">ID</th>
     <th runat="server">Description</th>
     <th runat="server">Qty</th>
     <th runat="server">UnitPrice</th>
     <th runat="server">Cost</th>
    </tr>
    <tr id="itemPlaceholder" runat="server"></tr>
  </table>
 </LayoutTemplate>
</asp:ListView>

So, you get to use normal asp.net controls, and "only" a cost of <//td>. so while the control "seems" a bit more messy, "when" you going to have lots of controls as opposed to just autogenerated columns, then I jump over to the listview.

I also googled for a decent style sheet, so the above markup produces this grid:

enter image description here

Not a big deal - but as a FYI? The more custom controls you start dropping into gridView? Well, if you plans are for lots of controls - then I quite much suggest listview since you don't need that itemtemplate stuff over and over.

Upvotes: 1

Related Questions