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

[Solved] Need help with RadGrid Custom Sorting with GridTemplateColumn

6 Answers 681 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sathish
Top achievements
Rank 1
Sathish asked on 17 Jun 2009, 07:50 PM
Hi,

I'm trying to use the custom sorting with my RadGrid. On SortCommand, I'm sorting the reocrds in db and rebinding the DataSource. After that, it's trying to run the default/internal sorting. Is there anyway to turn off the default sorting in RadGrid and just use the custom sorting?

Following is my sample code.

 

<telerik:RadGrid ID="grdStudentResult" runat="server" Width="99%"   
    AllowMultiRowSelection="false" AllowMultiRowEdit="false" AllowPaging="true"   
        AutoGenerateColumns="False" ShowStatusBar="False" PageSize="20"   
    GridLines="None" AllowSorting="true" Skin="Gray" BorderStyle="None" OnSortCommand="grdStudentResult_GridSortCommand" 
        OnNeedDataSource="grdStudentResult_NeedDataSource" OnItemDataBound="grdStudentResult_ItemDataBound">  
      
    <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="True" Position="TopAndBottom"   
                  PagerTextFormat="Records {2} - {3} of {5} | Displaying page {0} of {1} | {4}" /> 
 
 
    <HeaderStyle BackColor="White" CssClass="label" Font-Bold="True" Font-Underline="True" ForeColor="#222C80" />                  
    <MasterTableView Width="100%"  AutoGenerateColumns="False" DataKeyNames="StudentId" EditMode="InPlace" InsertItemDisplay="Bottom"   
        HeaderStyle-CssClass="label" BorderStyle="None" BackColor="White" HeaderStyle-ForeColor="#222C80" AlternatingItemStyle-BackColor="White"   
        HeaderStyle-Font-Size="11px" EditItemStyle-BackColor="#FFFF99" EditItemStyle-VerticalAlign="Middle"  AllowCustomSorting="true" AllowNaturalSort="false" AllowSorting="true" 
        CommandItemSettings-RefreshImageUrl="../../../App_Themes/Default/images/spacer.gif" NoMasterRecordsText="No records found for the selected search criteria.">  
 
        <CommandItemSettings RefreshText="" /> 
        <RowIndicatorColumn> 
            <HeaderStyle Width="20px"></HeaderStyle> 
        </RowIndicatorColumn> 
        <ExpandCollapseColumn> 
            <HeaderStyle Width="20px"></HeaderStyle> 
        </ExpandCollapseColumn> 
        <Columns> 
            <telerik:GridTemplateColumn HeaderText="Student Name" UniqueName="StudentName"  SortExpression="LastName" > 
                <ItemTemplate> 
                    <asp:LinkButton   
                            runat="server" 
                            ID="lnkStudentName"   
                            Text='' CSSClass="linkButton">  
                            </asp:LinkButton> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn HeaderText="Student ID" UniqueName="StudentID" SortExpression="StudentId" > 
                    <ItemTemplate><asp:Label runat="server" ID="lblStudentID" Text=''></asp:Label></ItemTemplate>  
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn HeaderText="Email Address" UniqueName="EmailAddress" SortExpression="EmailAddress" > 
                    <ItemTemplate> 
                        <asp:HyperLink CSSClass="linkButton" 
                                runat="server" 
                                ID="lnkEmailAddress"   
                                Text='<%# Eval("EmailAddress") %>'   
                                NavigateUrl=''>  
                                </asp:HyperLink> 
                    </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn HeaderText="Waiver Status"  UniqueName="WaiverStatus" SortExpression="RecentWaiverResponseStatus" > 
                    <ItemTemplate><asp:Label runat="server" ID="lblWaiverStatus" Text=''></asp:Label></ItemTemplate>  
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn HeaderText="Waiver Name"  UniqueName="WaiverName" SortExpression="RecentWaiverResponseName" > 
                    <ItemTemplate> 
                    <asp:HyperLink CSSClass="linkButton" 
                            runat="server" 
                            ID="lnkWaiverName"   
                            Text='<%# Eval("RecentWaiverResponseFormattedName") %>'   
                            NavigateUrl=''>  
                            </asp:HyperLink> 
                    </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn HeaderText="Coverage Status"  UniqueName="CoverageStatus" SortExpression="CoverageStatus" > 
                    <ItemTemplate><asp:Label runat="server" ID="lblCoverageStatus" Text='<%# Eval("CoverageStatus") %>'></asp:Label></ItemTemplate>  
            </telerik:GridTemplateColumn> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 
 

Thanks
Sathish

protected void grdStudentResult_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
{  
    if (Page.IsPostBack)  
    {  
        presenter.SearchStudents();  
    }   
}  
 
protected void grdStudentResult_GridSortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)  
{  
    if (e.Item.ItemType != GridItemType.Header)  
        return;  
 
    SortOrder sortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), e.NewSortOrder.ToString());  
    e.Item.OwnerTableView.DataSource = presenter.SortResult(e.CommandArgument.ToString(), sortOrder);  
    e.Item.OwnerTableView.Rebind();  
}  
 
protected void grdStudentResult_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
{  
 


6 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 19 Jun 2009, 08:57 AM
Hi Sathish,

You need to set e.Canceled = true; in your SortCommand event to prevent RadGrid's default sorting.

Regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Sathish
Top achievements
Rank 1
answered on 19 Jun 2009, 01:36 PM
Thanks Veli. Even I tried setting e.command = true, then it didn't show the ascending or descending image on the header. is there any way to get that working?

Thanks
Sathish
0
Veli
Telerik team
answered on 22 Jun 2009, 12:38 PM
Hello Sathish,

When using custom sorting, there are a few things that you need to ensure:

1. RadGrid is bound to a data source using its NeedDataSource event, but not to a automatic DataSource Controls (SlqDAtaSource, AccessDataSource, etc).

2. The AllowCustomSorting property of the respective GridTableView (e.g. MasterTableView) needs to be set to true.

3. You should not cancel your sort command if you need to preserve the sorting images.

Applying these settings, as demonstrated in this help article and this demo example, you can have RadGrid adjust its sorting column highlight and sorting image, without actualyl sorting the data, which is delegated to your implementation in RadGrid's SortCommand event.

Best wishes,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Sathish
Top achievements
Rank 1
answered on 22 Jun 2009, 01:39 PM
Thanks Veli.

Please look at my next reply
0
Sathish
Top achievements
Rank 1
answered on 22 Jun 2009, 03:21 PM

Provided Sample and Demo doesn't show how to change the sort expression in custom sorting. Can you please provide some sample for handling sort image in custom sorting?

I did try calling the follwoing code 
e.Item.OwnerTableView.SortExpressions.ChangeSortOrder(e.SortExpression); 

if (e.Item.ItemType != GridItemType.Header)  
   return;  
  
SortOrder sortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), e.NewSortOrder.ToString());  
e.Item.OwnerTableView.DataSource = presenter.SortResult(e.CommandArgument.ToString(), sortOrder);  
e.Item.OwnerTableView.Rebind();  
e.Canceled = true
e.Item.OwnerTableView.SortExpressions.ChangeSortOrder(e.SortExpression);  


This works fine in one column. When I sort a column and click  on another column to sort, this doesn't clear the previous columns sort image.. please look at the attached image.


Thanks
Sathish


0
Sathish
Top achievements
Rank 1
answered on 22 Jun 2009, 04:54 PM
Thanks Veli for your assistence.

I fixed by enabling custom Paging and I didn't not cancel the sorting after custom sorting in SortCommand. 

Sample code for others
In SortCommand 
if (e.Item.ItemType != GridItemType.Header) 
   return
 
SortOrder sortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), e.NewSortOrder.ToString()); 
int startIndex = grdStudentResult.CurrentPageIndex * grdStudentResult.PageSize; 
int maxSize = grdStudentResult.VirtualItemCount > grdStudentResult.PageSize ? grdStudentResult.PageSize : grdStudentResult.VirtualItemCount; 
e.Item.OwnerTableView.DataSource = presenter.SortResult(e.CommandArgument.ToString(), sortOrder).Skip(startIndex).Take(maxSize); 
e.Item.OwnerTableView.Rebind(); 

In NeedSource,
int startIndex = grdStudentResult.CurrentPageIndex * grdStudentResult.PageSize; 
int maxSize = value.Count > grdStudentResult.PageSize ? grdStudentResult.PageSize : value.Count; 
grdStudentResult.DataSource = value.Skip(startIndex).Take(maxSize); 

Tags
Grid
Asked by
Sathish
Top achievements
Rank 1
Answers by
Veli
Telerik team
Sathish
Top achievements
Rank 1
Share this question
or