locked
GridViewColumnHeader sort when clicked by date RRS feed

  • Question

  • Hi everyone 

    when I click one of gridviews column header then it sorts. However, when I click on the date or number formatted columns' headers then sorts by string. These columns which are not supported have right data type in the database(date,int,etc..) 

    My code is below. Please tell me what is wrong in my code. 

    Thanks in advance. 

    void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
            {            
                GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
                if (headerClicked == null)
                    return;
                headerClicked.Column.HeaderTemplate.DataType = typeof(System.DateTime);
                ListSortDirection direction;
                int idx = ((GridView)View).Columns.IndexOf(headerClicked.Column);
                if (headerClicked != null)
                {
                    if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                    {
                        if (headerClicked != _lastHeaderClicked)
                        {
                            direction = ListSortDirection.Ascending;
                            
                        }
                        else
                        {
                            if (_lastDirection == ListSortDirection.Ascending)
                            {
                                direction = ListSortDirection.Descending;
                            }
                            else
                            {
                                direction = ListSortDirection.Ascending;
                            }
                        }
                        Sort("[" + idx + "]", direction);
    
                        if (direction == ListSortDirection.Ascending)
                        {
                            headerClicked.Column.HeaderTemplate = FindResource("HeaderTemplateArrowUp") as DataTemplate;
                        }
                        else
                        {
                            headerClicked.Column.HeaderTemplate = FindResource("HeaderTemplateArrowDown") as DataTemplate;
                        }
    
                        // Remove arrow from previously sorted header
                        if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
                        {
                            _lastHeaderClicked.Column.HeaderTemplate = null;
                        }
                        _lastHeaderClicked = headerClicked;
                        _lastDirection = direction;
                    }
                }
            }
    
            private void Sort(string sortBy, ListSortDirection direction)
            {
                try
                {
                    ICollectionView dataView = CollectionViewSource.GetDefaultView(this.ItemsSource);
    
                    dataView.SortDescriptions.Clear();
                    SortDescription sd = new SortDescription(sortBy, direction);
                    dataView.SortDescriptions.Add(sd);
                    dataView.Refresh();
                }
                catch { }
    
            }


    KAdir

    • Moved by Caillen Tuesday, May 20, 2014 10:13 AM
    Monday, May 19, 2014 12:33 PM

Answers

All replies

  • HI 

    Might be you are formatting date or number in sql query as string There fore it is consider as string at datagridview

    In Order to sort datagridview on basis of date column use format property of datagridviewcoumn as 

    try for date column

    datagridview .Columns["DateColumnName"].DefaultCellStyle.Format = "yy/mm/dd";


    for number column

    datagridview .Columns["NoColumnName"].DefaultCellStyle.Format = "N2";


    Or

    DatagridViewColumn Dc as new DatagridViewColumn();
    Dc.DataPropertyName="DateColumnFromSql";
    Dc.HeaderText="Date";
    Dc.DefaultCellStyle.Format = "yy/mm/dd";
    DatagridView1.Columns.Add(Dc);

    For Number

    DatagridViewColumn Dc as new DatagridViewColumn();
    Dc.DataPropertyName="NumberColumnFromSql";
    Dc.HeaderText="Number";
    Dc.DefaultCellStyle.Format = "N2";
    DatagridView1.Columns.Add(Dc);

    ref 

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/10e7609c-1926-4983-b197-1cfe7c6f93d4/datagridview-sorting-by-date-column?forum=vbgeneral#776e0896-32ee-4d5f-8291-f4e2b7b8f64d


    Shridhar J Joshi Thanks a lot

    Monday, May 19, 2014 12:52 PM
  • Thanks for your response Shridhar. I checked my query and it is right. Also, I've casted these columns in query. However, still same. In addition when I try to apply your code below then it gives me error. 



    Monday, May 19, 2014 1:23 PM
  • HI

    What error you are getting?

    And donot cast or convert your columns in query

    Format it in  datagridview


    Shridhar J Joshi Thanks a lot

    Monday, May 19, 2014 1:36 PM
  • Please post ASP.NET questions in the ASP.NET forums (http://forums.asp.net).
    • Proposed as answer by Caillen Tuesday, May 20, 2014 10:12 AM
    • Marked as answer by Just Karl Wednesday, May 28, 2014 4:25 PM
    Monday, May 19, 2014 1:37 PM



  • Monday, May 19, 2014 1:47 PM