9 Replies - 524 Views - Last Post: 13 October 2019 - 08:16 PM Rate Topic: -----

#1 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 17-June 15

Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 02:50 AM

I have followed the https://docs.microso...etframework-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...d7mdhm94p/1.png

Is This A Good Question/Topic? 0
  • +

Replies To: Why is the dataGridView1_SortCompare function not working ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15332
  • View blog
  • Posts: 61,453
  • Joined: 12-June 08

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 05:30 AM

Please explain in what way it is not working.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7108
  • View blog
  • Posts: 24,142
  • Joined: 05-May 12

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 01:07 PM

(Looks like site picture uploads are broken again.) The picture the OP had for separate download was highlighting the row headers of the datagrid view. I think he was expecting the row headers to change order when the rows are sorted. I don't think that is the way the datagrid view works. I believe that it will sort the actual row data, but the row headers will still monotically increase from 1 to N.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15332
  • View blog
  • Posts: 61,453
  • Joined: 12-June 08

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 01:19 PM

Weird.. graphic looks to be jacked from a 2013 SO topic here on c++: https://stackoverflow.com/a/14915110

Dong's effort is severely lacking.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7108
  • View blog
  • Posts: 24,142
  • Joined: 05-May 12

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 01:34 PM

Based on other past posts, look like par for the course for him.
Was This Post Helpful? 0
  • +
  • -

#6 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 17-June 15

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 07:27 PM

Hi Skydiver
I have checked the link I sent to me to see if the image file is normal, maybe my internet is blocked, I want to number the dataGgridView but it does not work, you see the image file is circled in red rectangle Posted Image

Hi modi123_1
I have run your example send but the serial number it is located next to the column I want to chase a bit I have to do ? and the 6th line should be blank (the 6th line should not be numbered) what should I do ? You see my image file http://www.mediafire...f/dgv1.jpg/file

This post has been edited by dongtrien: 09 October 2019 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7108
  • View blog
  • Posts: 24,142
  • Joined: 05-May 12

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 08:07 PM

That part that you highlighted are the row headers. Those are not sorted as a result of any sorting events. They will always be in that order.

Also, how were we supposed to know that you were trying to sort serial numbers? You blurred out the serial numbers in the pictures, and you didn't show us any code that would hint to us the your are trying to sort serial numbers.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15332
  • View blog
  • Posts: 61,453
  • Joined: 12-June 08

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 09 October 2019 - 09:00 PM

That is _not_ his picture.

I have zero idea what "example of mine" you supposedly ran. I am still confused on what you are doing.

You haven't showed what you are actually doing.

This post is teetering on being binned for lack of effort.
Was This Post Helpful? 0
  • +
  • -

#9 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 17-June 15

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 13 October 2019 - 06:45 PM

the dataGridView1_SortCompare method 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;
        }

    }


and image file http://www.mediafire...i/dgv2.jpg/file
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7108
  • View blog
  • Posts: 24,142
  • Joined: 05-May 12

Re: Why is the dataGridView1_SortCompare function not working ?

Posted 13 October 2019 - 08:16 PM

As previously mentioned, the thing you are highlighting are the row headers. Those are not sorted. If you believe then should be sorted, which lines of code in your sort compare handler should be doing the sorting. Explain why you were expecting those lines to do the sorting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1