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

Sorting on a GridTemplateColumn

3 Answers 168 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aaron Gibbs
Top achievements
Rank 2
Aaron Gibbs asked on 17 Apr 2013, 02:14 PM
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?

3 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 22 Apr 2013, 07:56 AM
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.
0
Aaron Gibbs
Top achievements
Rank 2
answered on 22 Apr 2013, 04:12 PM
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.

0
Andrey
Telerik team
answered on 25 Apr 2013, 12:15 PM
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.
Tags
Grid
Asked by
Aaron Gibbs
Top achievements
Rank 2
Answers by
Andrey
Telerik team
Aaron Gibbs
Top achievements
Rank 2
Share this question
or