This is a migrated thread and some comments may be shown as answers.

Checkbox will not show in GridTemplateColumn

4 Answers 145 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 16 Sep 2011, 10:22 PM
I have a dataset coming back that has 2 columns with Y or N values that indicate whether someone received a packet and whether or not they attended an event.

I need to have a checkbox in the datagrid that is checked if the value is "Y" and not checked if the value is "N"
The problem I'm having is I can't even get the checkboxes to show up in the datagrid, and since they aren't there, when I try to set the checkbox in the code behind, the chkBox object is nothing, so it throws an error.

Here's my datagrid:
<telerik:RadGrid ID="RadGrid1" OnSortCommand="RadGrid1_SortCommand" EnableViewState="true" MasterTableView-RetrieveAllDataFields="false" MasterTableView-CanRetrieveAllData="false" EnableEmbeddedSkins="false" runat="server" AllowPaging="true" AllowSorting="true" Width="930px" Height="150px" PageSize="25">
    <ClientSettings ClientEvents-OnRowDblClick="dblClickRow" Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="false" Scrolling-AllowScroll="true" Scrolling-SaveScrollPosition="true" Scrolling-UseStaticHeaders="true"></ClientSettings>
    <SelectedItemStyle CssClass="grdSelectedRowStyleDefault" Font-Underline="false" />
    <MasterTableView AutoGenerateColumns="False" RetrieveAllDataFields="false" EnableNoRecordsTemplate="true" ShowHeadersWhenNoRecords="false">
        <PagerStyle CssClass="Label" Mode="NumericPages" HorizontalAlign="right"></PagerStyle>
        <HeaderStyle CssClass="grdHeaderStyleDefault" BackColor="#CE0042" ForeColor="White" ></HeaderStyle>
        <ItemStyle CssClass="grdRowStyleDefault"></ItemStyle>
        <AlternatingItemStyle CssClass="grdRowAlternateStyleDefault"></AlternatingItemStyle>
        <NoRecordsTemplate><table width="100%"><tr><td align="center"><asp:label runat="server" ID="lblNoRec" Font-Bold="false" Font-Size="small" ForeColor="Black" CssClass="label" BackColor="Gainsboro">No Data To Display.</asp:label></td></tr></table></NoRecordsTemplate>
        <Columns>
            <telerik:GridBoundColumn DataField="REGISTRATIONID" Display="false" DataType="System.String">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="EVENTID" Display="false" DataType="System.String">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ACCOUNTID" HeaderStyle-Width="75px" HeaderText="<span title='Account ID'>Account ID</span>" DataType="System.String">
                <ItemStyle Width="75px" Height="20px" BorderStyle="Solid" BorderWidth="1px" Wrap="false" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="LASTNAME" HeaderStyle-Width="150px" HeaderText="<span title='Last Name'>Last Name</span>" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Underline="false" DataType="System.String">
                <ItemStyle Width="150px" Height="20px" BorderStyle="Solid" BorderWidth="1px" Wrap="false" HorizontalAlign="Center" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FIRSTNAME" HeaderStyle-Width="135px" HeaderText="<span title='First Name'>Last Name</span>" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Underline="false" DataType="System.String">
                <ItemStyle Width="135px" Height="20px" BorderStyle="Solid" BorderWidth="1px" Wrap="false" HorizontalAlign="Center" />
            </telerik:GridBoundColumn>
            <telerik:GridNumericColumn  DataField="PLEDGEAMOUNT" HeaderStyle-Width="70px" HeaderText="<span title='Pledge Amount'>Pledge</span>" HeaderStyle-HorizontalAlign="Right" DataType="System.Decimal" NumericType="Currency">
                <ItemStyle Width="70px" Height="20px" HorizontalAlign="Right" BorderStyle="Solid" BorderWidth="1px" Wrap="false" />
            </telerik:GridNumericColumn >
            <telerik:GridNumericColumn  DataField="PAID" HeaderStyle-Width="70px" HeaderText="<span title='Paid'>Paid</span>" HeaderStyle-HorizontalAlign="Right" DataType="System.Decimal" NumericType="Currency">
                <ItemStyle Width="70px" Height="20px" HorizontalAlign="Right" BorderStyle="Solid" BorderWidth="1px" Wrap="false" />
            </telerik:GridNumericColumn >
            <telerik:GridBoundColumn DataField="TEAMCODE" HeaderStyle-Width="100px" HeaderText="<span title='Team Code'>Team Code</span>" DataType="System.String">
                <ItemStyle Width="100px" Height="20px" BorderStyle="Solid" BorderWidth="1px" Wrap="false" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="REGISTRATIONDATE" HeaderStyle-Width="75px" HeaderText="<span title='Registration Date'>Reg Date</span>" DataType="System.String">
                <ItemStyle Width="75px" Height="20px" BorderStyle="Solid" BorderWidth="1px" Wrap="false" />
            </telerik:GridBoundColumn>
             
    <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn1" HeaderStyle-Width="25px" HeaderStyle-HorizontalAlign="Center" HeaderText="<span title='Packet Sent'>P</span>">
        <EditItemTemplate>
            <asp:CheckBox ID="chkPACRECIND" AutoPostBack="false" runat="server" />
        </EditItemTemplate>
        <ItemStyle Width="25px" BorderStyle="Solid" BorderWidth="1px" />
    </telerik:GridTemplateColumn>
    <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn2" HeaderStyle-Width="25px" HeaderStyle-HorizontalAlign="Center" HeaderText="<span title='Attended'>A</span>">
        <EditItemTemplate>
            <asp:CheckBox Runat="server" ID="chkATTIND" />
        </EditItemTemplate>
        <ItemStyle Width="25px" BorderStyle="Solid" BorderWidth="1px" />
    </telerik:GridTemplateColumn>
 
 
            <telerik:GridBoundColumn DataField="PACKETSENTDATE" HeaderStyle-Width="75px" HeaderText="<span title='Packet Sent Date'>Packet Sent Date</span>" DataType="System.String">
                <ItemStyle Width="75px" Height="20" BorderStyle="Solid" BorderWidth="1px" Wrap="false" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BIBTEXT" HeaderStyle-Width="75px" HeaderText="<span title='Bib Number'>Bib Number</span>" DataType="System.String">
                <ItemStyle Width="75px" Height="20" BorderStyle="Solid" BorderWidth="1px" Wrap="false" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ATTENDEDIND" Display="false" DataType="System.String">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PACKETRECEIVEDIND" Display="false" DataType="System.String">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

The last 2 columns are hidden and contain the Y/N value. I can get that value in the ItemDataBound Sub, but I can't find the checkbox, so the chkBox.Checked = True/False step errors.  If I skip setting the checkbox value, the grid loads, and I see the two checkbox columns, but the columns are empty.

lstr_PacketReceived = item("PACKETRECEIVEDIND").Text
Dim
chkBox As CheckBox = DirectCast(e.Item.FindControl("CheckBox1"), CheckBox)
If Not (lstr_PacketReceived Is Nothing) Then
    If lstr_PacketReceived.Equals("Y") Then
        chkBox.Checked = True
    Else
        chkBox.Checked = False
    End If
End If

Any idea what I'm doing wrong here?  I'm sure it's something simple, I just can't seem to figure this on out.
Thanks

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Sep 2011, 05:46 AM
Hello,

Please see below Code Snippet.
<telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn1" HeaderStyle-Width="25px" HeaderStyle-HorizontalAlign="Center" HeaderText="<span title='Packet Sent'>P</span>">
        <ItemTemplate>
            <asp:CheckBox ID="chkPACRECIND" AutoPostBack="false" runat="server" />
        </ItemTemplate>
        <ItemStyle Width="25px" BorderStyle="Solid" BorderWidth="1px" />
    </telerik:GridTemplateColumn>

Note : use ItemTemplate inplace of EditItemTemplate

Thanks,
Jayesh Goyani
0
Tim
Top achievements
Rank 1
answered on 19 Sep 2011, 02:59 PM
Thanks for the reply, but after changing to ItemTemplate from EditItemTemplate, the checkboxes still do not show in the datagrid.

Radgrid screenshot

0
Andrey
Telerik team
answered on 21 Sep 2011, 05:31 AM
Hi Tim,

In order to display controls in RadGrid while you are not in Edit mode, you should add them to the Item template rather than EditItemTemplate as suggested in the previous post. There should not be a problem with displaying checkboxes in Item Template of RadGrid's template column.
I have created sample project to illustrate how to use GridTemplateColumn with checkbox in its ItemTemplate.

I am attaching a project. Check it out and see what are the differences between the sample project and yours.

Best wishes,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Tim
Top achievements
Rank 1
answered on 21 Sep 2011, 03:51 PM

I finally figured out what the problem was late last night.

I was getting frustrated that my controls wouldn’t show up on the radgrid, so I put a standard ASP grid on the page.

Sure enough, the checkboxes showed up when the page loaded. 
 

But then, when I clicked page 2 of the asp grid (the bottom grid), after the postback, the controls showed up on the radgrid!  
 

As it turns out, one of our developers, had come across a bug in the RadGrid where it will display &nbsp; as the text of an empty column.

He found mention of it here on the Telerik site along with a snippet of code to change the "&nbsp;" to an empty string:

 

Dim gridItem As GridDataItem = CType(item, GridDataItem)
For Each column As GridColumn In RadGrid1.MasterTableView.RenderColumns
    gridItem(column.UniqueName).Text = If(gridItem(column.UniqueName).Text = " ", "", gridItem(column.UniqueName).Text)
Next

 

We’ve been putting this in all our ItemDataBound methods. 

So what was happening was, for the checkbox column, it looked at the column and saw that the text of the column was “”, which is correct, since there’s no text there, it’s just a checkbox.  And since there was no "&nbsp;" as the text of the column, it just set the value of the column back to itself, which in this case was an empty string.


When it did that, it overwrote the checkbox with the empty string, so on Page_Load, there was nothing but an empty string in those two columns.  When I did the postback on the ASP grid, the radgrid ItemDataBound method didn’t get called, so the checkboxes appeared. 

We modified the telerik snippet so that it won’t wipe out any controls we happen to put in a grid.

Thanks so much for all the help and feedback.

Tim

Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Tim
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or