开发者

How to sort this?

I have a datatable populated with a nullable year column of datatyp开发者_Python百科e int. In addition, every time the year changes, I have added a blank row.

DataTable table = new DataTable();
table.Columns.Add("Year", typeof(int));
table.Columns["Year"].AllowDBNull = true;

Example of table contents:

2005
2005
DBNull.Value
2006
DBNull.Value
2007

On a row by row basis and only being able to look at the current row and the next row, how would I sort this so that the DBNull.Value rows still separate the difference in years?

Thanks!


I don't like this sort of design in general, but here is how you could do it.

Add another column of type double, which will be hidden in the display, and call it "SortYear". For the rows with years in them, put the year in this hidden column. For the null rows put the previous year plus 0.5. Then sort by SortYear, then by whatever column you really want to sort by other than year.

Year          SortYear  ServiceDescription (or whatever)
2005          2005.0    Internet Service
2005          2005.0    Upgraded Cap Limits
DBNull.Value  2005.5
2006          2006.0    Internet Service
DBNull.Value  2006.5
2007          2007.0    Internet Service



order by SortYear, ServiceDescription


You need another column to perform the actual sort on. Use a timestamp or something like that, so you can sort in the order they were inserted, but display the Year column the same as you do now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜