Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why is the dataGridView1_SortCompare function not working ?

Why is the dataGridView1_SortCompare function not working ?

Scheduled Pinned Locked Moved C#
csharpcomjsonquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2456424
    wrote on last edited by
    #1

    I have followed the [DataGridViewSortCompareEventHandler Delegate (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewsortcompareeventhandler?view=netframework-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 [http://www.mediafire.com/file/pkkrtvd7mdhm94p/1.png\](http://www.mediafire.com/file/pkkrtvd7mdhm94p/1.png)

    L 1 Reply Last reply
    0
    • U User 2456424

      I have followed the [DataGridViewSortCompareEventHandler Delegate (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewsortcompareeventhandler?view=netframework-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 [http://www.mediafire.com/file/pkkrtvd7mdhm94p/1.png\](http://www.mediafire.com/file/pkkrtvd7mdhm94p/1.png)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What do you mean by "column header sort doesn't work"? Please show your sort method, and explain which part of it has the problem.

      U 1 Reply Last reply
      0
      • L Lost User

        What do you mean by "column header sort doesn't work"? Please show your sort method, and explain which part of it has the problem.

        U Offline
        U Offline
        User 2456424
        wrote on last edited by
        #3

        I want to number the dataGgridView but it doesn't work, you see the red rectangular image file

        L 1 Reply Last reply
        0
        • U User 2456424

          I want to number the dataGgridView but it doesn't work, you see the red rectangular image file

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The image you have posted is not viewable and cannot be downloaded without creating a Mediafire account. Please post the actual code in your original question and explain what the error is and where it occurs.

          U 1 Reply Last reply
          0
          • L Lost User

            The image you have posted is not viewable and cannot be downloaded without creating a Mediafire account. Please post the actual code in your original question and explain what the error is and where it occurs.

            U Offline
            U Offline
            User 2456424
            wrote on last edited by
            #5

            I will post code does not work, I will post my code for you to see, what am I wrong with ?

            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;
                }
            
            }
            

            image file http://www.mediafire.com/view/9ogb4uwvfr2ag3i/dgv2.jpg/file

            L 1 Reply Last reply
            0
            • U User 2456424

              I will post code does not work, I will post my code for you to see, what am I wrong with ?

              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;
                  }
              
              }
              

              image file http://www.mediafire.com/view/9ogb4uwvfr2ag3i/dgv2.jpg/file

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well, you still have not explained what part of this does not work. However, I would query why you are converting numeric values to strings in order to compare them. You will certainly get the wrong results when you have more than 9 rows. Ah, I see you have copied this code direct from the MSDN page at DataGridViewSortCompareEventArgs Class (System.Windows.Forms) | Microsoft Docs[^].

              U 1 Reply Last reply
              0
              • L Lost User

                Well, you still have not explained what part of this does not work. However, I would query why you are converting numeric values to strings in order to compare them. You will certainly get the wrong results when you have more than 9 rows. Ah, I see you have copied this code direct from the MSDN page at DataGridViewSortCompareEventArgs Class (System.Windows.Forms) | Microsoft Docs[^].

                U Offline
                U Offline
                User 2456424
                wrote on last edited by
                #7

                all the code I copied from microsoft company, even the top of the page I posted without you reading my first questions, now you give the same link from microsoft company. Before you criticize others, have you tried my example ?

                L 1 Reply Last reply
                0
                • U User 2456424

                  all the code I copied from microsoft company, even the top of the page I posted without you reading my first questions, now you give the same link from microsoft company. Before you criticize others, have you tried my example ?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I posted that link to emphasise the point that you were using the Microsoft code. However, you have still, despite three requests, not explained what your problem is. [edit] And to answer your question, yes I have tried the example, and it works fine. [/edit]

                  U 1 Reply Last reply
                  0
                  • L Lost User

                    I posted that link to emphasise the point that you were using the Microsoft code. However, you have still, despite three requests, not explained what your problem is. [edit] And to answer your question, yes I have tried the example, and it works fine. [/edit]

                    U Offline
                    U Offline
                    User 2456424
                    wrote on last edited by
                    #9

                    I found out why the dataGridView1_SortCompare method doesn't allow dataGridView1.DataSource to be assigned if using the sort feature.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups