How to sort a DataGridView column (bound to a BindingSource) by date?

I have a DataGridView that, among others, has columns containing dates. Unfortunately, the dates are in DD.MM.YYYY format, and the whole value is in 1 column, which is the common date format in Europe. DGV is tied to BindingSource, which is able to sort and improve sorting.

The problem is this: if I just use standard DGV sorting, the dates will be displayed as strings (they are displayed in a DataGridViewTextBoxColumn) and therefore sorted by day-> month-> year, but of course I "I need the exact opposite; I I want them to be sorted in chronological order.

So, is there a way to sort these columns the way I want them?

  • The easiest way would be to use the SortCompare DGV event, but apparently this cannot be done if the DGV is bound to a DataSoruce.
  • Of course, I used Google, and I always get "using the Sort property for advanced sorting." The BindingSource, which is tied to DGV, really supports sorting and advanced sorting, but as I understand it, it just gives me the opportunity, for example, to sort by multiple columns and not provide a way to sort the date column by year-> month-> day (or in more general terms allows me to implement a comparison function). Or am I missing something?

What options should I fulfill, what do I want? When explaining, please keep in mind that I have nothing new in programming - new to this Windows Forms material.

Thanks in advance!

+4
source share
2 answers
private void DataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e) { if (e.Column.Name == "YourColumnName") //<== this must be your date column name { DateTime dt1 = Convert.ToDateTime(e.CellValue1); DateTime dt2 = Convert.ToDateTime(e.CellValue2); e.SortResult = System.DateTime.Compare(dt1, dt2); e.Handled = true; } } 
+1
source

Perhaps this code will give you the ideal. First I set up the data:

 DataTable dt = new DataTable(); dt.Columns.Add("DateOfBirth", typeof(DateTime)); dt.Rows.Add(new DateTime(1981, 10, 29)); dt.Rows.Add(new DateTime(1984, 8, 12)); dt.Rows.Add(new DateTime(1982, 9, 7)); bindingSource1.DataSource = dt; dataGridView1.DataSource = bindingSource1; dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic; 

Now, using ColumnHeaderMouseClick, we will sort by the first column:

 private SortOrder _sortDirection; private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.ColumnIndex == 0) { string sort = string.Empty; switch (_sortDirection) { case SortOrder.None: _sortDirection = SortOrder.Ascending; sort = "asc"; break; case SortOrder.Ascending: _sortDirection = SortOrder.Descending; sort = "desc"; break; case SortOrder.Descending: _sortDirection = SortOrder.None; sort = string.Empty; break; } dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = _sortDirection; if (!string.IsNullOrEmpty(sort)) { bindingSource1.Sort = "DateOfBirth " + sort; } else { bindingSource1.RemoveSort(); } } } 

Hope this helps!

0
source

Source: https://habr.com/ru/post/1399338/


All Articles