1
votes

I have a GridCheckboxColumn in my grid and a need to create a GridTemplateColumn according to the checkbox is checked or not. For instance,

line 1,

if the checkbox is checked the GridTempleColumn label displays "YES" , if the checkbox is not checked the GridTempleColumn label displays "NO", if the checkbox is NULL : it displays "N/A".

To sum up, i have this in my aspx page:

<telerik:GridCheckBoxColumn DataField="facturable" DataType="System.Boolean" HeaderText="facturable"
    SortExpression="facturable" UniqueName="facturable">
</telerik:GridCheckBoxColumn>

                <telerik:GridTemplateColumn  HeaderText="Type de tickets"
    UniqueName="typedestickets">
    <ItemTemplate><asp:Label id="test" runat="server"></asp:Label></ItemTemplate>
</telerik:GridTemplateColumn>

My try in code behind : ( not working )

protected void RadGrid1_DataBound(object sender, EventArgs e)
{
    foreach (Telerik.Web.UI.GridDataItem dataItem in RadGrid1.MasterTableView.Items)
    {
        CheckBox chkDelete = (CheckBox)dataItem.FindControl("facturable"); 
        Label label = (Label)dataItem.FindControl("test");

        if (chkDelete.Checked == true) { label.Text = "MA"; }
    }



}

Thank you in advance for your help

3
Solution : CheckBox checkboxfacturable = (CheckBox)dataItem["facturable"].Controls[0]; Label label = (Label)dataItem["typedestickets"].Controls[0]; - user1173169
If that is indeed the solution then please add it as an answer and accept it so this does not sit around as unanswered. - kevev22

3 Answers

2
votes

Check "e.item" is GridDataItem before accessing your controls. I perform a similar check in an application I'm working on, except I'm using "OnItemDatabound".

protected void grdSummary_OnItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        // Access your controls here
    }

}
2
votes
CheckBox checkboxfacturable = (CheckBox)dataItem["facturable"].Controls[0]; 
Label label = (Label)dataItem["typedestickets"].Controls[0];
1
votes

Most controls, including grids, can bind a variety of objects, not just your data items. You should always check that e.Item is of the correct type first thing in your OnItemDataBound handler before using it.