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

Set the row colour dependant on checkbox column

3 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 05 Sep 2012, 09:50 AM
Hi,
I am wishing to change the colour of a row if it is 'urgent' in my internal message system.
I found this code...

Protected Sub RadGrid1_ItemDataBound(ByVal sender As System.Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    'Change the color of inactive rows
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim Urgent As CheckBox = DirectCast(item.FindControl("Urgent"), CheckBox)
        If Urgent.Checked = True Then
            e.Item.BackColor = Drawing.Color.RoyalBlue
        End If
    End If
End Sub


and changed it to suit my grid, or so I believed; however it does not work properly, and throws an exception ("Object reference not set to an instance of an object.") when checking whether

"If Urgent.Checked = true then"

What am I doing wrong, the code for my radgrid is...

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateSelectColumn="True" CellSpacing="0"
            GridLines="Horizontal" AutoGenerateColumns="False" OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged"
            OnNeedDataSource="RadGrid1_NeedDataSource" AllowAutomaticUpdates="True" AllowPaging="True"
            AllowSorting="True" ToolTip="Click on a message to view it" >
            <SortingSettings EnableSkinSortStyles="false" />
            <ClientSettings EnablePostBackOnRowClick="true" EnableRowHoverStyle="True">
                <Selecting AllowRowSelect="true" />
            </ClientSettings>
            <MasterTableView DataKeyNames="UniqueID" PageSize="20" Width="100%">
                <Columns>
                    <telerik:GridBoundColumn DataField="UniqueID" HeaderText="UniqueID" UniqueName="UniqueID"
                        FooterStyle-Width="1px" HeaderStyle-Width="1px" ItemStyle-Width="1px" Visible="false"
                        DataType="System.Int32" ReadOnly="True" SortExpression="UniqueID">
                        <FooterStyle Width="1px"></FooterStyle>
                        <HeaderStyle Width="1px"></HeaderStyle>
                        <ItemStyle Width="1px"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Customer" HeaderText="Customer" UniqueName="Customer"
                        FooterStyle-Width="14%" HeaderStyle-Width="14%" ItemStyle-Width="14%" FilterControlAltText="Filter Customer column"
                        SortExpression="Customer">
                        <FooterStyle Width="14%"></FooterStyle>
                        <HeaderStyle Width="14%"></HeaderStyle>
                        <ItemStyle Width="14%"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Subject" HeaderText="Subject" UniqueName="Subject"
                        FooterStyle-Width="46%" HeaderStyle-Width="46%" ItemStyle-Width="46%" SortExpression="Subject">
                        <FooterStyle Width="46%"></FooterStyle>
                        <HeaderStyle Width="46%"></HeaderStyle>
                        <ItemStyle Width="46%"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Taken By" UniqueName="Name"
                        FooterStyle-Width="8%" HeaderStyle-Width="8%" ItemStyle-Width="8%" DataType="System.Int32"
                        FilterControlAltText="Filter TakenByID column" SortExpression="TakenByID">
                        <FooterStyle Width="8%"></FooterStyle>
                        <HeaderStyle Width="8%"></HeaderStyle>
                        <ItemStyle Width="8%"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="TakenOn" HeaderText="TakenOn" UniqueName="TakenOn"
                        FooterStyle-Width="11%" HeaderStyle-Width="11%" ItemStyle-Width="11%" DataType="System.DateTime"
                        FilterControlAltText="Filter TakenOn column" SortExpression="TakenOn">
                        <FooterStyle Width="11%"></FooterStyle>
                        <HeaderStyle Width="11%"></HeaderStyle>
                        <ItemStyle Width="11%"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="Urgent" DataType="System.Boolean" FilterControlAltText="Filter Urgent column"
                        HeaderText="Urgent" SortExpression="Urgent" UniqueName="Urgent" FooterStyle-Width="7%"
                        HeaderStyle-Width="7%" ItemStyle-Width="7%">
                        <FooterStyle Width="7%"></FooterStyle>
                        <HeaderStyle Width="7%"></HeaderStyle>
                        <ItemStyle Width="7%"></ItemStyle>
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="StatusNew" HeaderText="Status ID" UniqueName="StatusNew"
                        FooterStyle-Width="11%" HeaderStyle-Width="11%" ItemStyle-Width="11%" SortExpression="StatusID">
                        <FooterStyle Width="11%"></FooterStyle>
                        <HeaderStyle Width="11%"></HeaderStyle>
                        <ItemStyle Width="11%"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"
                        HeaderText="Delete" ButtonType="ImageButton" ImageUrl="~/RadControls/Grid/Skins/Default/Delete.gif"
                        FooterStyle-Width="4%" HeaderStyle-Width="4%" ItemStyle-Width="4%" >
                        <FooterStyle Width="4%"></FooterStyle>
                        <HeaderStyle Width="4%"></HeaderStyle>
                        <ItemStyle Width="4%"></ItemStyle>
                    </telerik:GridButtonColumn>
                </Columns>
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="5px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="5px"></HeaderStyle>
                </ExpandCollapseColumn>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
        </telerik:RadGrid>


and it comes from a sql database

Thanks in advance
Ryan

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Sep 2012, 10:09 AM
Hi Ryan,

Try accessing the CheckBoxColumn as shown below.
VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim Urgent As CheckBox = DirectCast(item("Urgent").Controls(0), CheckBox)
        If Urgent.Checked = False Then
            e.Item.BackColor = System.Drawing.Color.RoyalBlue
        End If
    End If
End Sub

Thanks,
Princy.
0
Ryan
Top achievements
Rank 1
answered on 05 Sep 2012, 11:23 AM
Thanks Princy
Works a treat, one more question, sorry I forgot to add this bit...
just as I "DirectCast" the item as a CheckBox
How would I do the same for an integer
Something like this...

Dim Status As Int32 = Integer.Parse(item("StatusNew").Controls(0), Integer)

But that doesn't work, how would I do it?
The end result that I want to achieve is that before seeing whether urgent.checked = false
I first want to check whether "StatusNew" = 0

Thanks once again
Ryan
0
Princy
Top achievements
Rank 2
answered on 05 Sep 2012, 11:37 AM
Hi Ryan,

Try the following code snippet.

VB:
Dim Status As Int32 = Convert.ToInt32(item("StatusNew").Text)

Thanks,
Princy.
Tags
Grid
Asked by
Ryan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ryan
Top achievements
Rank 1
Share this question
or