dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Why is the dataGridView1_SortCompare function not working ?

  1. #1
    Join Date
    Sep 2007
    Posts
    332

    Why is the dataGridView1_SortCompare function not working ?

    I have followed the https://docs.microsoft.com/en-us/dot...tframework-4.8 document but the dataGridView1_SortCompare column header sort doesn't work ? Although I have declared

    this.dataGridView1.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(this.dataGridView1_SortCompare);

    you see my attached file
    Attached Images Attached Images  

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,100

    Re: Why is the dataGridView1_SortCompare function not working ?

    Thanks for embedding the image. What code do you havd inside the sort compare handler?

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,323

    Re: Why is the dataGridView1_SortCompare function not working ?

    The only thing in that picture I can make out is the column which is sorted properly so I really have no idea what you are talking about.
    Maybe show some code and show an example of where it is not working or give a detailed description of the problem you are having. Saying it don't work could mean virtually anything.

    Also note that the line of code you posted is C# not VB.Net but you have posted in VB.Net
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Sep 2007
    Posts
    332

    Re: Why is the dataGridView1_SortCompare function not working ?

    I posted the wrong post through vb.net, thanks to the admin who switched to C # .net for help, but I read the vb.net code also to understand.
    the dataGridView1_SortCompare method does not work, I will post my code for you to see, what am I wrong with ?
    Code:
    public partial class Form1 : Form
       {
            public Form1()
            {
                InitializeComponent();            
    
                //sort colum header
                this.dataGridView1.VirtualMode = false;
                this.dataGridView1.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(this.dataGridView1_SortCompare);
                
                dataGridView1.DataSource = CreateTable(5);
                
            }
    
            private static DataTable CreateTable(int RowCount)
            {
                DataTable tbl = new DataTable();
                tbl.Columns.Add("check1", typeof(bool));
                tbl.Columns.Add("ID", typeof(int));
                tbl.Columns.Add("Name", typeof(string));
                tbl.Columns.Add("Number", typeof(int));
                tbl.Columns.Add("Date", typeof(DateTime));
                tbl.Columns.Add("check2", typeof(bool));
                for (int i = 0; i < RowCount; i++)
                    tbl.Rows.Add(new object[] { false, i, String.Format("Name{0}", i), 3 - i, DateTime.Now.AddDays(i), true });
                return tbl;
            }
               
            private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
            {
                // Try to sort based on the cells in the current column.
                e.SortResult = System.String.Compare(
                    e.CellValue1.ToString(), e.CellValue2.ToString());
    
                // If the cells are equal, sort based on the ID column.
                if (e.SortResult == 0 && e.Column.Name != "ID")
                {
                    e.SortResult = System.String.Compare(
                        dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
                        dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
                }
                e.Handled = true;
            }
    
        }
    and image file [LINK REMOVED]
    Last edited by Arjay; October 13th, 2019 at 10:24 PM. Reason: Removed link

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,100

    Re: Why is the dataGridView1_SortCompare function not working ?

    Please don't post image file links.

    What happens when you step through the sort handler method in a debugger?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)