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

Using the sorting properties in a radgrid

1 Answer 284 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 01 Jun 2011, 02:46 PM
I am trying use the sorting properties in the radgrid, and I am wondering if there is a way to sort the data bound to the grid without hitting the database again.  My problem is when I do not hit the database and attempt to sort, my data in the grid disappears.

My radgrid is set up to look like this:
<radG:RadGrid ID="CustomerGrid" runat="server" AutoGenerateColumns="false" Width="100%" EnableAJAX="True" OnPageIndexChanged="SearchResults_PageIndexChanged" 
    Skin="Office2007" AllowFilteringByColumn="False" AllowPaging="True" PageSize="35" ShowStatusBar="True" ShowFooter="False" 
    ShowGroupPanel="False" GridLines="None" CellPadding="1" AllowSorting="True" OnSortCommand="RadGrid1_SortCommand">
    <PagerStyle Mode="NextPrevAndNumeric" />
    <MasterTableView EditMode="InPlace" ShowFooter="True" Width="99%" TableLayout="Auto" AllowMultiColumnSorting="true" AllowNaturalSort="true">
        <Columns>
            <radG:GridBoundColumn DataField="CustomerID" HeaderText="Customer Number" SortExpression="CustomerID" UniqueName="CustomerID">
            </radG:GridBoundColumn>
            <radG:GridBoundColumn DataField="CustomerName" HeaderText="Customer Name" SortExpression="CustomerName" UniqueName="CustomerName">
            </radG:GridBoundColumn>
            <radG:GridTemplateColumn HeaderText="Status" HeaderStyle-Width="160px" ItemStyle-Width="160px">
                <ItemTemplate>
                    <%# GetStatus(Eval("CustomerID")) %>
                </ItemTemplate>
            </radG:GridTemplateColumn>
            <radG:GridBoundColumn DataField="Effective" HeaderText="Effective">
            </radG:GridBoundColumn>
        </Columns>
        <ExpandCollapseColumn Visible="False">
            <HeaderStyle Width="19px" />
        </ExpandCollapseColumn>
        <RowIndicatorColumn Visible="False">
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
    </MasterTableView>
    <ClientSettings ReorderColumnsOnClient="True" AllowDragToGroup="True" AllowColumnsReorder="True">
        <Scrolling AllowScroll="True" UseStaticHeaders="True"></Scrolling>
    </ClientSettings>
</radG:RadGrid>

My code behind methods for sorting and loading the radgrid are shown below:
protected void SearchResults_PageIndexChanged(object source, Telerik.WebControls.GridPageChangedEventArgs e)
{
    LoadData();
}
  
protected void RadGrid1_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
{
    if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))
    {
        GridSortExpression sortExpr = new GridSortExpression();
        sortExpr.FieldName = e.SortExpression;
        sortExpr.SortOrder = GridSortOrder.Ascending;
  
        e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
    }
  
}

The LoadData method is used to get the results from the database and returns a data set which I bind to the radgrid.

The Sort method I tried using from the demo, I found here:
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/sorting/defaultcs.aspx


From this I would like to be able to sort all of my columns, except for the effective column, in ascending or descending order depending on what the user selects.  I was also curious if you could offer up any assitance as to how to go about sorting my Item Template column, if that is possible.  Since, my CustomerID is bound to the column and I just use code to display the customer's status, I was not sure if this was possible to sort by the actual status name.

Any assistance would be appreciated. Thanks.

Joe

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 06 Jun 2011, 08:39 AM
Hello Joe,

Sorting is one of the operations that require data-binding/rebinding for the RadGrid. If you do not want to hit the databse on each sort command, you can save the grid data source in Session, ViewState or Cach and assign the saved/cached data source to the grid.

For more infromation on how to perform custom sorting for a GridTemplateColumn, you can refer to the below articles:
http://www.telerik.com/help/aspnet-ajax/grid-sorting-for-hyperlink-template-columns.html
http://www.telerik.com/help/aspnet-ajax/grid-apply-custom-sort-criteria.html

Kind regards,
Iana
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or