Sorting on a GridTemplateColumn

4 posts, 0 answers
  1. Aaron Gibbs
    Aaron Gibbs avatar
    23 posts
    Member since:
    Dec 2007

    Posted 17 Apr 2013 Link to this post

    Good morning,

    This is not an issue but more asking for advice . . .

    I have a RadGrid with several GridTemplateColumns that contain a label.  In the code-behind page's RadGrid_ItemDataBound event, I have some logic that will set the text of this label based on several conditions, such as this:

    Protected Sub rgStoresMissingFiles_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles rgStoresMissingFiles.ItemDataBound
        If e.Item.ItemType = Telerik.Web.UI.GridItemType.Item Or e.Item.ItemType = Telerik.Web.UI.GridItemType.AlternatingItem Then
            Dim lblConnectionStatus As Label = e.Item.FindControl("lblConnectionStatus")
            Dim lblNextPollTime As Label = e.Item.FindControl("lblNextPollTime")
            Dim lblLastConnectTime As Label = e.Item.FindControl("lblLastConnectTime")
            Dim warningcolor As Drawing.Color = Drawing.Color.FromArgb(244, 244, 185)
            Dim errorcolor As Drawing.Color = Drawing.Color.FromArgb(253, 125, 249)
     
            With e.Item.DataItem
                Dim strLastConnectTime As String = CType(e.Item.DataItem("last_connect_time"), DateTime).ToString("hh:mm tt")
                Dim strNextConnectTime As String = CType(e.Item.DataItem("poll_now_time"), DateTime).ToString("hh:mm tt")
     
                If e.Item.DataItem("currently_polling") Then
                    lblConnectionStatus.Text = "Currently polling"
                ElseIf e.Item.DataItem("last_connect_time") > Now().ToShortDateString Then
                    lblLastConnectTime.Text = strLastConnectTime
                    Select Case e.Item.DataItem("last_connect_status_id")
                        Case 3 ' NoConnect
                            lblConnectionStatus.Text = "Unable to connect to store"
                            lblNextPollTime.Text = "Queued"
                            e.Item.BackColor = errorcolor
                        Case 2 ' ConnectNoData
                            lblConnectionStatus.Text = "Potential line problem"
                            lblNextPollTime.Text = strNextConnectTime
                            e.Item.BackColor = errorcolor
                        Case 1 ' ConnectWithData
                            If e.Item.DataItem("count_staged_archives") > 0 Then
                                lblConnectionStatus.Text = "Data polled"
                                lblNextPollTime.Text = "Data staged"
                            ElseIf e.Item.DataItem("count_staged_archives") = 0 And e.Item.DataItem("count_loaded_archives") > 0 Then
                                lblConnectionStatus.Text = "Connected, missing data"
                                lblNextPollTime.Text = strNextConnectTime
                                e.Item.BackColor = warningcolor
                            End If
                    End Select
                ElseIf e.Item.DataItem("poll_now") Then
                    lblConnectionStatus.Text = "Staged for first poll"
                    lblNextPollTime.Text = strNextConnectTime
                    lblLastConnectTime.Text = "N/A"
                Else
                    lblConnectionStatus.Text = "Staged for first poll"
                    lblNextPollTime.Text = "Queued"
                    lblLastConnectTime.Text = "N/A"
                End If
            End With
     
        End If
    End Sub

    I would like for my users to be able to sort on these GridTemplateColumns but not sure how to do this.  There is no single field in the datasource I can use for the sort, as you can see above, the values shown in the columns are dependent on several columns.

    How can I get the RadGrid to sort on the actual value shown in the column?
  2. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 22 Apr 2013 Link to this post

    Hello,

    By design RadGrid applies the filtering, paging, sorting, grouping and almost any other advanced functionality directly to the DataBase.

    So, as this help topic says you will need to set the DataField property of the GridTemplateColumn in order to use the Sort and Filter functionality.

    In your case, you may need to create an additional field in the database that will be used for this template column. Is the data that you are applying to the Label controls somehow dependent on other fields of the datasource?

    If you could elaborate a bit more on your scenario, we could find other solutions as well.

    Kind regards,
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Aaron Gibbs
    Aaron Gibbs avatar
    23 posts
    Member since:
    Dec 2007

    Posted 22 Apr 2013 Link to this post

    Thank you for your response.  The logic to determine what is displayed in the GridTemplateColumn is complex as you can see from the code above, the text changes based on several values present in several columns.

    I will add another DataColumn to the DataSet I am binding to, and put this logic somewhere else, where it will store the value to be displayed in the DataSet directly and I can use a normal GridBoundColumn on the grid.

  5. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 25 Apr 2013 Link to this post

    Hello,

    Yes this approach should be sufficient to achieve your goal.

    Do not hesitate to contact us again if you need assistance while using our controls.

    Kind regards,
    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.
Back to Top