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

radgrid Group & sort

5 Answers 427 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Erik
Top achievements
Rank 2
Erik asked on 10 Nov 2008, 01:52 PM
 Hi all,

I have a grid with 3 columns, a name and a date and a calculated (sql) field witch displays terms like "last week, this week, today, yesterday, next week, last year, etc..."

When i use a grouping al works fine, only the group sorting is alphabetical (of course) and thus the wrong order. I would like the grid toe group, but sort the groups logical, on the date field.

any ideas?

Thanks in advance!

5 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 12 Nov 2008, 01:34 PM
Hi Proovit,

Can you please provide some more details on the calculated column? Is it calculated in the database (it resides there), or is it calculated via a separate query, or perhaps dynamic code when the grid is created?

Regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Erik
Top achievements
Rank 2
answered on 18 Nov 2008, 11:25 AM
Hi Yavor,

The calculation is a database stored procedure en produces the texts like "yesterday", "Tomorrow", "Las week", etc.

Regards,

Erik
0
Yavor
Telerik team
answered on 21 Nov 2008, 06:19 AM
Hi Proovit,

In this case, you will only be able to sort in the groups in ascending and descending order, based on the field coming from the database. Another option is to include another column, but not display it, which would contain the "correct" sorting values for the original column, and set the sort expression to it.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Regin
Top achievements
Rank 1
answered on 07 Aug 2012, 05:36 AM
Hi Yovor,

How to set the sort expression?

Regards,
Regin
0
Joe
Top achievements
Rank 1
answered on 14 Sep 2012, 03:07 PM

Here is an example if anyone was having issues with this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;

namespace WebApplication1
{
  public partial class _Default : System.Web.UI.Page
  {
    public SortedList<string, int> sortingOrder;
    protected void Page_Load(object sender, EventArgs e)
    {
      ScriptManager sm = ScriptManager.GetCurrent(this.Page);
      if (sm == null)
      {
        sm = new ScriptManager();
        Page.Form.Controls.Add(sm);
      }

      RadGrid rg = new RadGrid();
      rg.ItemDataBound += new GridItemEventHandler(rg_ItemDataBound);
      Page.Form.Controls.Add(rg);

      DataTable dt = new DataTable();

      //Colmns
      dt.Columns.Add("Title");
      dt.Columns.Add("Notes");
      dt.Columns.Add("Person");
      dt.Columns.Add("Category");
      dt.Columns.Add("RowOrder");

      //Rows
      dt.Rows.Add("Open Task Item", "Ship something", "John Smith", "Orders");
      dt.Rows.Add("Another Task Item", "Ship something", "J Rogers", "Orders");
      dt.Rows.Add("Thank You", "Send thank you letter", "John Smith", "Misc");
      dt.Rows.Add("Thank you again", "The Store", "J Person", "Misc");
      dt.Rows.Add("Buy Items", "Buy Something", "The Store", "Purchases");
      dt.Rows.Add("Title", "Buy Something again", "The Store", "Purchases");
      dt.Rows.Add("Test", "Buy Something", "The Store", "Purchases");

      // Data Source for Sorting
      sortingOrder = new SortedList<string, int>();
      sortingOrder.Add("Orders", 1);
      sortingOrder.Add("Purchases", 2);
      sortingOrder.Add("Misc", 3);

      foreach (DataRow dr in dt.Rows)
      {
        int order;
        switch (dr["Category"].ToString())
        {
          case "Orders":
            sortingOrder.TryGetValue("Orders", out order);
            dr["RowOrder"] = order;
            break;
          case "Purchases":
            sortingOrder.TryGetValue("Purchases", out order);
            dr["RowOrder"] = order;
            break;
          case "Misc":
            sortingOrder.TryGetValue("Misc", out order);
            dr["RowOrder"] = order;
            break;
          default:
            break;
        }
      }

      rg.GroupingEnabled = true;
      rg.MasterTableView.GroupByExpressions.Add(new GridGroupByExpression("RowOrder Group By RowOrder"));

      rg.DataSource = dt;
      rg.DataBind();
    }
    protected void rg_ItemDataBound(object sender, GridItemEventArgs e)
    {
      //Change Header Test
      if (e.Item is GridGroupHeaderItem)
      {
        GridGroupHeaderItem i = e.Item as GridGroupHeaderItem;

        //Get the Value from the header
        int order = Convert.ToInt32(i.DataCell.Text.Replace("RowOrder: ", string.Empty));

        foreach (var r in sortingOrder)
        {
          if (order == r.Value)
            i.DataCell.Text = r.Key;
        }
      }
    }
  }
}

Tags
Grid
Asked by
Erik
Top achievements
Rank 2
Answers by
Yavor
Telerik team
Erik
Top achievements
Rank 2
Regin
Top achievements
Rank 1
Joe
Top achievements
Rank 1
Share this question
or