Skip to content

Infragistics Community Forum / Desktop / Ultimate UI for Windows Forms / Click an ultragrid's one column header to sort by another

Click an ultragrid's one column header to sort by another

New Discussion
Ahmad Traboulsi
Ahmad Traboulsi asked on Sep 6, 2018 5:26 PM

hi,

let’s say we have the following columns:

account name (ex: Mega Design) << string

created on (ex: 15) << integer

description (ex: 15 days ago) << string

if we click on account name column header, the grid sorts the column asc/desc according to its datatype (in this case string).

if we click on (description) column header, it will will sort the column as a string (15 days ago).

now, i want to hide (created on) column and keep (account name, description)

and make clicking on description sorts the grid as an integeras if we have clicked on (created on)).

is that possible?

Sign In to post a reply

Replies

  • 0
    Mike Saltzman
    Mike Saltzman answered on Oct 16, 2017 3:03 PM

    Hi, 

    Yes, this is actually pretty easy. There is a SortComparer property on the UltraGridColumn. So this allows you to customize the sorting of any column however you want. In this case, it's even easier, because you aren't doing any complex sorting, you just want to re-direct the sorting of one column to use the values from another column.

    I have attached a small sample project that sorts a string column ("String 1") by the value of an integer column ("Int32 1").

    • 0
      Ahmad Traboulsi
      Ahmad Traboulsi answered on Oct 17, 2017 10:16 AM

      Hi Mr. saltzman,

      that's EXACTLY what i wanted, it works like a charm.

      thanks a lot.

    • 0
      Erin Haltom
      Erin Haltom answered on Sep 6, 2018 4:02 PM

      Can you post the code here? I'm unable to open the file.

      • 0
        Alan Halama
        Alan Halama answered on Sep 6, 2018 5:26 PM

        Here is the relevant SortComparer from Mike's sample:

        internal class MySortComparer : IComparer
        {
            int IComparer.Compare(object x, object y)
            {
                // x and y will be UltraGridCell. So cast them to the correct type. 
                var xCell = (UltraGridCell)x;
                var yCell = (UltraGridCell)y;
        
                // Get the rows
                var xRow = xCell.Row;
                var yRow = yCell.Row;
        
                // Get the value of the "Int32 1" cell in each row. 
                // If there is a chance that the value will be null or 
                // DBNull, you will need to check for that here. 
                int xInt = (int)xRow.Cells["Int32 1"].Value;
                int yInt = (int)yRow.Cells["Int32 1"].Value;
        
                // Return the comparison of the two integer values. 
                return xInt.CompareTo(yInt);
            }
        }

        And the logic from the InitializeLayout method where the class is instantiated:

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            var layout = e.Layout;
            var band = layout.Bands[0];
            var ov = layout.Override;
        
            ov.HeaderClickAction = HeaderClickAction.SortMulti;
            band.Columns["String 1"].SortComparer = new MySortComparer();
        }

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Ahmad Traboulsi
Favorites
0
Replies
4
Created On
Sep 06, 2018
Last Post
7 years, 5 months ago

Suggested Discussions

Created by

Created on

Sep 6, 2018 5:26 PM

Last activity on

Feb 24, 2026 12:42 PM