Ticket Q100566
Visible to All Users
Duplicate

Sort the grid at database level!

created 17 years ago

Hi,
I have asked this question in the user forum with no luck!:
http://community.devexpress.com/forums/t/63171.aspx
And I have looked at issues that other users have, for example:
http://www.devexpress.com/Support/Center/p/Q99212.aspx?searchtext=sort&tid=4b2d6f97-c4ae-48fc-87f6-8c5da6541e40&pid=22713479-a995-45d4-9ed4-72ffa096d83d
http://www.devexpress.com/Support/Center/p/Q97780.aspx?searchtext=sort&tid=4b2d6f97-c4ae-48fc-87f6-8c5da6541e40&pid=22713479-a995-45d4-9ed4-72ffa096d83d
I'm not the only one with sorting problems!:
http://www.devexpress.com/Support/Center/SearchResults.aspx?searchtext=sorting&tid=4b2d6f97-c4ae-48fc-87f6-8c5da6541e40&pid=22713479-a995-45d4-9ed4-72ffa096d83d
Is it not possible to make a simle sort in database with your grid???
Could you please, please add sorting events in future releases like MS have!!!
The closest solution I have reached is in one of links above:

  1. Turn off callbacks: EnableCallBacks="False"
  2. In postback event, loop through the columns:
                if (IsPostBack)
                {
                    GridViewDataColumn dataColumn = null;
                    int i = 0;
                    string columnName = String.Empty;
                    while (i < gvUser.Columns.Count)
                    {
                        dataColumn = (GridViewDataColumn)gvUser.Columns[i];
                        if (dataColumn == null)
                        {
                            i += 1;
                            continue;
                        }
                        if (dataColumn.SortOrder != DevExpress.Data.ColumnSortOrder.None)
                        {
                            columnName = dataColumn.FieldName;
                            break;
                        }
                        i += 1;
                    }
                    Response.Write(columnName);
                }
    The problem with this code is I have to click twice on the header to get the correct sort name.
    Is this the best solution? And do you know why I have to click twice to get the correct name?
Show previous comments (6)
DevExpress Support Team 17 years ago

    Hi Mattias,
    We have discussed your question and decided to implement additional events. Please let us know which events you would like to see in the ASPxGridView.
    Thanks,
    Plato

    MJ MJ
    Mattias Johansson 17 years ago

      Plato, that is so good news! :)
      What I need is an event when I click on a header.
      I was just evaluating and testing the Teleriks gridview and they have an event called SortCommand.
      I'm needing just about the same as this:
              protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e)
              {
                  if ("Name".Equals(e.CommandArgument))
                  {
                      e.Canceled = true;
                      GridSortExpression expression = new GridSortExpression();
                      expression.FieldName = "Name";
                      switch (e.OldSortOrder)
                      {
                          case GridSortOrder.None:
                          case GridSortOrder.Descending:
                              expression.SortOrder = GridSortOrder.Ascending;
                              e.Item.OwnerTableView.DataSource = GetData("Name", true, skip, take);
                              //Response.Write("asc");
                              break;
                          case GridSortOrder.Ascending:
                              expression.SortOrder = GridSortOrder.Descending;
                              e.Item.OwnerTableView.DataSource = GetData("Name", false, skip, take);
                              //Response.Write("desc");
                              break;
                      }
                      RadGrid1.CurrentPageIndex = 0;
                      e.Item.OwnerTableView.SortExpressions.AddSortExpression(expression);
                      e.Item.OwnerTableView.Rebind();
                  }
              }

              private IList<Swh.Wb8.Entities.Category> GetData(string sortColumn, bool sortOrder, int skip, int take)
              {
                  IGenericDao<Swh.Wb8.Entities.Category> Dao = new GenericDao<Swh.Wb8.Entities.Category>();
                  IList<Swh.Wb8.Entities.Category> list = Dao.GetObjects(new Order(sortColumn, sortOrder), skip, take, out recordCount);
                  RadGrid1.VirtualItemCount = recordCount;
                  return list;
              }
      What happens is:
      When I click a header I'll get the clicked column name and order(asc/desc) and sends the values to the database (GetData)
      GetData returns the new rows and binds it to the grid.
      Telerik also have the superb feature of what they call: VirtualItemCount.
      As you see from my IList<T> from NHibernate I have an output parameter (recordcount) with the total rows in database.
      I send in the recordcount into the grid so it can caluculate the paging mechanism. Fantastic! :)
      If you can fix this you will be my hero! :)
      Best Regards
      Mattias

      DevExpress Support Team 17 years ago

        Hi Matthias,
        I have registered the <Sclink id="S19661" /> suggestion on your behalf. You will be notified when its status is changed.
        Thanks,
        Plato

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.