Why is the dataGridView1_SortCompare function not working ?
-
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)
-
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)
-
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.
I want to number the dataGgridView but it doesn't work, you see the red rectangular image file
-
I want to number the dataGgridView but it doesn't work, you see the red rectangular image file
-
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.
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
-
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
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[^].
-
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[^].
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 ?
-
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 ?
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]
-
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]
I found out why the dataGridView1_SortCompare method doesn't allow dataGridView1.DataSource to be assigned if using the sort feature.