2

I am using a DataGridView control in WinForms to populate data. While sorting it, it considers blank rows as well. I want to move those blank lines to the bottom of the grid. Whatever the sort criteria is.

Please help to do this?

This is on the similar lines like DataGridView sorting with nulls in DateTime column

But the problem is if DataGridView is DataBound or its VirtualMode property is set to true. SortCompare event doesn't work.

Community
  • 1
  • 1
Vijay Balkawade
  • 3,574
  • 12
  • 49
  • 87
  • Why do you have 'blank rows' ? – leppie Dec 06 '11 at 06:57
  • User can add and edit rows. So the feature is like this. – Vijay Balkawade Dec 06 '11 at 07:14
  • That fails to answer my question. Please define 'blank row' exactly. – leppie Dec 06 '11 at 07:16
  • Let's say there is a grid control which is having columns as, EmployeeName, Address, contact no. It has got 5 records in it. Now the feature allows user to add blank rows to enter new employee records and save them back to DB. Now, if grid is sorted it considers those blank rows as well. – Vijay Balkawade Dec 06 '11 at 07:24
  • OK, I think I understand. They are bound to an object, but the object contains no values? – leppie Dec 06 '11 at 08:20
  • From [MSDN](http://msdn.microsoft.com/en-us/library/95scxcdy.aspx): "To customize sorting for columns bound to an external data source, you must use the sorting operations provided by the data source. In virtual mode, you must provide your own sorting operations for unbound columns." Check [this question](http://stackoverflow.com/questions/1699642/how-to-sort-databound-datagridview-column), I hope it can help in your situation. – akoso Dec 06 '11 at 15:27
  • try DataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic; it may solve your problem – Prakash Kunwar Mar 19 '12 at 12:02

1 Answers1

0

You can do something like this on your data access

if the sort is ASCENDING - ORDER BY (CASE WHEN EmployeeName IS NULL then 'Z' ELSE EmployeeName END)

if the sort is DESCENDING - ORDER BY (CASE WHEN EmployeeName IS NULL then 'A' ELSE EmployeeName END)

Jervie Vitriolo
  • 348
  • 4
  • 13