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

Grid Cell Colors Moving When Grouping is Used

1 Answer 87 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nathan
Top achievements
Rank 1
Nathan asked on 06 Jun 2014, 02:59 PM
I am using the "NeedDataSource" method to fill in a grid.  On the ItemDataBound I am setting the background colors for some values in a couple of column cells.  (Sub below)

This displays properly.  Dragging columns around works properly and the colors follow the cells.  However, when you do a grouping by dragging the column to the top of the grid the colors do not stay with the cells.

I have attached a before and after image...

Any ideas why this is moving?   I am setting the colors based on the FindByUniqueName identifier for the column.

Thanks,
Nathan


Protected Sub rgDispatch_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles rgDispatch.ItemDataBound
    '//Is it a GridDataItem
    If Not IsNothing(TryCast(e.Item, Telerik.Web.UI.GridDataItem)) Then
        'Get the instance of the right type
        Dim dataBoundItem As Telerik.Web.UI.GridDataItem = DirectCast(e.Item, Telerik.Web.UI.GridDataItem)
        'Set the desired color on the Status column based on the value
        Dim StatusColIndex As Integer = rgDispatch.Columns.FindByUniqueName("Status").OrderIndex
        Select Case dataBoundItem("Status").Text
            Case "Loading"
                dataBoundItem("Status").BackColor = System.Drawing.Color.Yellow
            Case "Loaded"
                dataBoundItem("Status").BackColor = System.Drawing.Color.DodgerBlue
            Case "Claimed"
                dataBoundItem("Status").BackColor = System.Drawing.Color.Orange
            Case "Started"
                dataBoundItem("Status").BackColor = System.Drawing.Color.FromArgb(19, 229, 19)  'Green color
            Case "Stopped"
                dataBoundItem("Status").BackColor = System.Drawing.Color.IndianRed
        End Select
        'Set the desired color on the Status column based on the value
        Dim MatReadyColIndex As Integer = rgDispatch.Columns.FindByUniqueName("MatReady").OrderIndex
        Select Case dataBoundItem("MatReady").Text
            Case "Yes"
                e.Item.Cells(MatReadyColIndex).BackColor = System.Drawing.Color.FromArgb(19, 229, 19)
            Case "No"
                e.Item.Cells(MatReadyColIndex).BackColor = System.Drawing.Color.IndianRed
        End Select
    End If
    If Not IsNothing(TryCast(e.Item, Telerik.Web.UI.GridPagerItem)) Then
        Dim pager As Telerik.Web.UI.GridPagerItem = DirectCast(e.Item, Telerik.Web.UI.GridPagerItem)
        Dim PagerLabel As Label = pager.FindControl("ChangePageSizeLabel")
        PagerLabel.Text = "Items per Page"
    End If
End Sub

1 Answer, 1 is accepted

Sort by
0
Nathan
Top achievements
Rank 1
answered on 06 Jun 2014, 03:07 PM

Just as soon as I asked the question.....

Fixed it.  I noticed that the "Status" column colors were not moving.  So, the right way to set the colors is using the "dataBoundItem" reference instead of the "e.Item.Cells".

So, here is the fixed sub....

Maybe this will help someone!!

Protected Sub rgDispatch_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles rgDispatch.ItemDataBound
    '//Is it a GridDataItem
    If Not IsNothing(TryCast(e.Item, Telerik.Web.UI.GridDataItem)) Then
        'Get the instance of the right type
        Dim dataBoundItem As Telerik.Web.UI.GridDataItem = DirectCast(e.Item, Telerik.Web.UI.GridDataItem)
        'Set the desired color on the Status column based on the value
        Dim StatusColIndex As Integer = rgDispatch.Columns.FindByUniqueName("Status").OrderIndex
        Select Case dataBoundItem("Status").Text
            Case "Loading"
                dataBoundItem("Status").BackColor = System.Drawing.Color.Yellow
            Case "Loaded"
                dataBoundItem("Status").BackColor = System.Drawing.Color.DodgerBlue
            Case "Claimed"
                dataBoundItem("Status").BackColor = System.Drawing.Color.Orange
            Case "Started"
                dataBoundItem("Status").BackColor = System.Drawing.Color.FromArgb(19, 229, 19)  'Green color
            Case "Stopped"
                dataBoundItem("Status").BackColor = System.Drawing.Color.IndianRed
        End Select
        'Set the desired color on the Status column based on the value
        Dim MatReadyColIndex As Integer = rgDispatch.Columns.FindByUniqueName("MatReady").OrderIndex
        Select Case dataBoundItem("MatReady").Text
            Case "Yes"
                'e.Item.Cells(MatReadyColIndex).BackColor = System.Drawing.Color.FromArgb(19, 229, 19)
                dataBoundItem("MatReady").BackColor = System.Drawing.Color.FromArgb(19, 229, 19)
            Case "No"
                'e.Item.Cells(MatReadyColIndex).BackColor = System.Drawing.Color.IndianRed
                dataBoundItem("MatReady").BackColor = System.Drawing.Color.IndianRed
        End Select
    End If
    If Not IsNothing(TryCast(e.Item, Telerik.Web.UI.GridPagerItem)) Then
        Dim pager As Telerik.Web.UI.GridPagerItem = DirectCast(e.Item, Telerik.Web.UI.GridPagerItem)
        Dim PagerLabel As Label = pager.FindControl("ChangePageSizeLabel")
        PagerLabel.Text = "Items per Page"
    End If
End Sub


Tags
Grid
Asked by
Nathan
Top achievements
Rank 1
Answers by
Nathan
Top achievements
Rank 1
Share this question
or