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

[Solved] Set sort from code behind

3 Answers 614 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam Lindsay
Top achievements
Rank 1
Adam Lindsay asked on 15 Dec 2009, 03:50 AM
Everything I have read on this forum and in the samples says this should work.  I have the following code in C# page_load:

SearchResultGrid.MasterTableView.CurrentPageIndex = 0;

 

GridSortExpression ge = new GridSortExpression();

 

ge.FieldName =

"CustomerID";

 

ge.SortOrder =

GridSortOrder.Descending;

 

SearchResultGrid.MasterTableView.SortExpressions.Add(ge);

SearchResultGrid.Rebind();

The code above sorts the data in the grid with no issues... However, it does not select the column and visually show that it is sorted.  Here is the grid code:

 

<

 

telerik:RadGrid ID="SearchResultGrid" EnableAJAX="False" ShowStatusBar="True" PageSize="25" Width="99%" AllowPaging="True"

 

 

AllowSorting="True" runat="server" Skin="Green" EnableEmbeddedSkins="false" GridLines="Both" OnNeedDataSource="SearchResultGrid_NeedDataSource">

 

 

<clientsettings allowkeyboardnavigation="True">

 

 

<Selecting AllowRowSelect="false" />

 

 

<Resizing AllowColumnResize="True" AllowRowResize="False" />

 

 

<Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="false" />

 

 

<ClientEvents OnActiveRowChanged="ActiveRowChanged" OnRowSelected="RowSelected" OnKeyPress="GridKeyPressed"/>

 

 

</clientsettings>

 

 

<mastertableview autogeneratecolumns="false" clientdatakeynames="CustomerID,SiteID">

 

 

<Columns>

 

 

<telerik:GridTemplateColumn UniqueName="CustomerID" HeaderText="Customer" SortExpression="CustomerId" >

 

 

<itemtemplate>

 

 

<a href="javascript:document.getElementById('SearchTracker').value = '0';CloseWindowWithArgument('<%# Eval("CustomerID")+"','"+ Eval("SiteID") %>');"><%# Eval("CustomerId") %></a>&nbsp;

 

 

</itemtemplate>

 

 

</telerik:GridTemplateColumn>

 

 

<telerik:GridBoundColumn Visible="false" HeaderText="Customer" UniqueName="ExportCustomerID" DataField="CustomerId" DataFormatString="{0}&nbsp;" SortExpression="CustomerId"/>

 

 

<telerik:GridBoundColumn HeaderText="Site Id" HeaderStyle-Width="5%" DataField="SiteID" UniqueName="SiteID" DataFormatString="{0}&nbsp;" SortExpression="SiteID" />

 

 

<telerik:GridBoundColumn HeaderText="Customer Name" HeaderStyle-Width="30%" DataField="CustomerName" UniqueName="CustomerName" SortExpression="CustomerName" />

 

 

<telerik:GridBoundColumn HeaderText="Address" HeaderStyle-Width="30%" DataField="AddressLine1" UniqueName="Address" SortExpression="AddressLine1" />

 

 

<telerik:GridBoundColumn HeaderText="City" HeaderStyle-Width="20%" DataField="City" UniqueName="City" SortExpression="City" />

 

 

<telerik:GridBoundColumn HeaderText="State" HeaderStyle-Width="5%" DataField="State" UniqueName="State" SortExpression="State" />

 

 

</Columns>

 

 

<NoRecordsTemplate>

 

 

<div class="NoRecordText">No records to display.</div>

 

 

</NoRecordsTemplate>

 

 

</mastertableview>

 

 

<pagerstyle mode="NumericPages" AlwaysVisible="true" />

 

 

</telerik:RadGrid>

The grid is in a table cell, in a div.  How can I get the grid to visually show the sort expression?

Thanks,

- Adam

 

3 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 15 Dec 2009, 06:26 PM
I believe this might resolve the issue:

 
SearchResultGrid.MasterTableView.CurrentPageIndex = 0; 
GridSortExpression ge = new GridSortExpression(); 
ge.FieldName = "CustomerID"
ge.SortOrder = GridSortOrder.Descending; 
SearchResultGrid.MasterTableView.SortExpressions.Add(ge); 
SearchResultGrid.MasterTableView.Rebind(); 


Basically you just need to define that you are rebinding the MasterTableView - I think that should do it :)
0
Adam Lindsay
Top achievements
Rank 1
answered on 16 Dec 2009, 03:31 AM
Thanks for the response Schlurk but, I did try that with no luck. 
0
Radoslav
Telerik team
answered on 17 Dec 2009, 12:09 PM
Hi Adam,

I reviewed your code and I think that the problem is caused by SortExpression="CustomerId" on TemplateColumn. The sort expression is case sensitive and your data field is named CustomerID, so in your case the SortExpression should be SortExpression="CustomerID" . I am sending you an example based on your code. I hope this helps.


Sincerely yours,
Radoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Adam Lindsay
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Adam Lindsay
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or