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

[RadGrid] Sorting Column by Clicking on Header Column

1 Answer 244 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jean-Marie
Top achievements
Rank 1
Jean-Marie asked on 20 Sep 2013, 08:27 AM
Hi there ,

i'd like (i guess) something easy but still enough hard for me :)

I'm looking for sorting a raadgrid by clicking on Header Columns.

This is my radgrid : 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TodoGrid.ascx.cs" Inherits="CSN.Phenix.UI.Web.Foundation.CommonControls.TodoList.TodoGrid" %>
<telerik:RadGrid ID="rg" runat="server" AutoGenerateColumns="false" GridLines="None"
    AllowSorting="true" AllowPaging="true" AllowCustomPaging="true"
    PagerStyle-Mode="NextPrevAndNumeric" PagerStyle-NextPageText="Next"
    PagerStyle-PrevPageText="Previous" PageSize="10" PagerStyle-Position="Bottom"
    OnNeedDataSource="rg_NeedDataSource" OnItemDataBound="rg_ItemDataBound"
    meta:resourcekey="resourceRgTodo" >
    <MasterTableView DataKeyNames="Id" DataMember="Id">
    </MasterTableView>
</telerik:RadGrid>

I'm generating DataColumn dynamically in code behind : 

private void CreateGridColumnsGeneric()
      {
          int hiddenColumnsCount = 0;
          this.specialColumns = new List<SpecialColumn>();
           
          foreach (DataColumn dataColumn in this.DataSourceObject.GetColumns(this.TabId))
          {
              if (dataColumn.ColumnName == PhxTools.TodoList.SPECIAL_DISPLAY_COLUMN_NAME)
              {
                   
                  // colonne qui permet de savoir si l'on doit afficher la ligne d'une certaine manière
                  // on conserve la colonne pour pouvoir gérer l'affichage de la ligne correctement
                  this.specialColumns.Add(new SpecialColumn()
                  {
                      ColumnName = PhxTools.TodoList.SPECIAL_DISPLAY_COLUMN_NAME,
                      ColumnType = "Special",
                      ColumnDisplayIndex = dataColumn.Ordinal - hiddenColumnsCount
                  });
              }
              else if (dataColumn.ExtendedProperties[PhxTools.TodoList.TODO_GRID_COLUMN_HIDDEN] == null)
              {
                  GridColumn column = null;
 
                  if (dataColumn.ExtendedProperties[PhxTools.TodoList.TODO_GRID_COLUMN_TYPE] != null)
                  {
                      string colType = dataColumn.ExtendedProperties[PhxTools.TodoList.TODO_GRID_COLUMN_TYPE].ToString();
 
                      // les colonnes "hyperliens"
                      if (colType == PhxTools.TodoList.TODO_GRID_COLUMN_HYPERLINK)
                          column = CreateHyperLinkColumn(dataColumn);
                      // les colonnes "images"
                      else if (colType == PhxTools.TodoList.TODO_GRID_COLUMN_IMAGE)
                      {
                          // on conserve la colonne pour pouvoir gérer l'affichage de la ligne correctement
                          this.specialColumns.Add(new SpecialColumn()
                          {
                              ColumnName = dataColumn.ColumnName,
                              ColumnType = PhxTools.TodoList.TODO_GRID_COLUMN_IMAGE,
                              ColumnDisplayIndex = dataColumn.Ordinal - hiddenColumnsCount
                          });
                          column = CreateImageColumn(dataColumn);
                      }
                  }
                  else
                  {
                      // les autres colonnes : on ne fait que du binding
                      column = CreateColumn(dataColumn);
                  }
 
                  if (dataColumn.ExtendedProperties[PhxTools.TodoList.COLUMN_SORT_EXPRESSION] != null)
                      column.SortExpression = dataColumn.ExtendedProperties[PhxTools.TodoList.COLUMN_SORT_EXPRESSION].ToString();
                  else
                      column.SortExpression = null;
 
                  column.HeaderText = dataColumn.ColumnName;
                  if (dataColumn.ExtendedProperties[PhxTools.TodoList.TODO_COLUMN_DISPLAY_NAME] != null)
                      column.HeaderText = dataColumn.ExtendedProperties[PhxTools.TodoList.TODO_COLUMN_DISPLAY_NAME].ToString();
 
                  if (column != null)
                      rg.Columns.Add(column);
              }
              else
              {
                  ++hiddenColumnsCount;
              }
          }
      }

What could i need to add for sorting them ?
Adding SortExpression? How could i do it in code behind ?


Thx in advance :)

J-Marie P. 


Edit : 

Its seems sorting work for the X rows already fill in datasource. ( excepted Imagecolumn and hyperlink colum )
I need to sort on All of my data ,i already have a GetData retriveing without sortedField.So i have to do a GetDataByField() to retrive X first rows then each time i click on header recall this method and give me the X good rows :)

How could i catch click on header ? 

 

1 Answer, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 25 Sep 2013, 10:21 AM
Hello,

I would suggest you to review the help topics below for more information on this matter:
http://www.telerik.com/help/aspnet-ajax/grid-basic-sorting.html
http://www.telerik.com/help/aspnet-ajax/grid-sorting-expressions.html
http://www.telerik.com/help/aspnet-ajax/grid-sorting-for-hyperlink-template-columns.html

Regards,
Maria Ilieva
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Jean-Marie
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Share this question
or