There is a DataGridView to which the table is bound via the DataSource

 dgvManInfo.DataSource = new DataView(ds.Tables["ManInfo"]); 

A column has been added to the table.

 ds.Tables["ManInfo"].Columns.Add("Дата рождения", typeof(string), String.Format("IIF([Birthday] = '{0}', 'Не задано', " + "SUBSTRING(CONVERT([Birthday], System.String), 1, 11))", DATE_DONT_KNOWN)); 

intended to conveniently display the date or the value "not set" when the date is equal to the specified value.
There is a problem: sorting now works, naturally, in rows!
Invalid DataColumn Sort
When clicking on the Дата рождения column, it is necessary to sort by the Birthday column.
Through the event handler, the DataGridView.SortCompare does not work, because

This event occurs only when the P: System.Windows.Forms.DataGridView.DataSource is set and the P: System.Windows.Forms.DataGridView.VirtualMode property value is false.
https://msdn.microsoft.com/ru-ru/library/system.windows.forms.datagridview.sortcompare(v=vs.110).aspx

    1 answer 1

    I propose to create a new column of type DateTime - then it will automatically have the correct sorting.

     ds.Tables["ManInfo"].Columns.Add("Дата рождения", typeof(DateTime), "[Birthday]"); 

    To output the Not specified value when necessary, you can handle the CellFormatting event:

     private void DgvManInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { var dgv = (DataGridView)sender; if (dgv.Columns[e.ColumnIndex].Name == "Дата рождения") { var dt = e.Value as DateTime?; if (dt == DATE_DONT_KNOWN) e.Value = "Не задано"; } } 

    I assume at the same time that DATE_DONT_KNOWN is of type DateTime .

    Additionally, you can assign the desired output format for the column:

     dgvManInfo.Columns["Дата рождения"].DefaultCellStyle.Format = "yyyy-MM-dd"; 
    • And the display speed will not suffer much from this? - Alexey Stepanov
    • @ AlekseyStepanov - I think processing a column with an IIF...SUBSTRING...CONVERT expression IIF...SUBSTRING...CONVERT equivalent to handling an event. The code can be slightly improved (accelerated) by using the column index instead of the name (comparing strings is much harder than comparing numbers). - Alexander Petrov