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

Inherited GridBoundColumn sorting not working

2 Answers 31 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mustafa
Top achievements
Rank 1
Mustafa asked on 07 Aug 2014, 12:48 PM
Hi, I have a custom gridcolumn inherited from GridBoundColumn. All columns are sortable in my grid except this.
I couldn't find why. Could you help me?


Public Class GridActivePassiveColumn
        Inherits GridBoundColumn
   ...
End Class

<%--Sorting doesn't work--%>
<myCompany:GridActivePassiveColumn HeaderText="Free of Charge" DataField="FlFoc" SortExpression="FlFoc">
    <HeaderStyle Width="35px" />
    <ItemStyle HorizontalAlign="Center" />
</myCompany:GridActivePassiveColumn>
 
<%--Sorting works--%>
<telerik:GridBoundColumn HeaderText="Free of Charge" DataField="FlFoc" SortExpression="FlFoc">
    <HeaderStyle Width="35px" />
    <ItemStyle HorizontalAlign="Center" />
</telerik:GridBoundColumn>

2 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 12 Aug 2014, 08:28 AM
Hello Mustafa,

I have tested a simple scenario on my end with inherited GridBoundColumn and the sorting works as expected.

Nevertheless, as a wild guess, if you are overriding the InitializeCell method of the GridBoundColumn, setting the header item Text property for displaying the column's will cause a problems with the sorting, because when sorting is enabled, the header cell should contain a LinkButton, which will initiate the sorting on click. Following is a simple example demonstrating how to add LinkButton to the header cell which will sort the column correctly:
Public Overrides Sub InitializeCell(cell As System.Web.UI.WebControls.TableCell, columnIndex As Integer, inItem As Telerik.Web.UI.GridItem)
    If TypeOf inItem Is GridHeaderItem Then
        If Not inItem.OwnerTableView.AllowSorting Then
            cell.Text = Me.DataField
        Else
            Dim button As New LinkButton()
            button.Text = Me.DataField
            button.OnClientClick = "Telerik.Web.UI.Grid.Sort($find('" + inItem.OwnerTableView.ClientID + "'), '" + Me.SortExpression + "'); return false;"
            cell.Controls.Add(button)
        End If
    End If
    ....
End Sub

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mustafa
Top achievements
Rank 1
answered on 12 Aug 2014, 12:05 PM
Hi Konstantin;

Your guess was right. My custom gridbound column only needs to change item cells not headers.

I fixed my problem with this code. Thanks.
Public Overrides Sub InitializeCell(ByVal cell As System.Web.UI.WebControls.TableCell, ByVal columnIndex As Integer, ByVal inItem As Telerik.Web.UI.GridItem)
        If TypeOf inItem Is GridHeaderItem Then
            MyBase.InitializeCell(cell, columnIndex, inItem)
        End If
        If TypeOf inItem Is GridDataItem Then
            'Do the job.
        End If
    End Sub

Tags
Grid
Asked by
Mustafa
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Mustafa
Top achievements
Rank 1
Share this question
or