开发者

DataView Sort - how to when column names are unknown

I need advice on how to go about a problem I have with sorting a DataView in C#.

I need to add a sort property to my DataView because I later need it when I call a Find on the DataView (which needs to have a sort property specified in order to work). I need the Find in order to properly map a DataRow to a DataGridViewRow. I currently just use the row index to map to the DataGridViewRow, but this won't work in all cases if the user physically moves rows, or changes the sort order in the DataGridView (UI) because then the ids no longer match-up/map.

When I create the DataView, I load in a DataTable to pass in for creation. This DataTable can be of any form, I do not know what the DataTable contains for columns and rows. Therefore, I cannot create a sort if I don't know any column names to sort by.

I was thinking of just taking the first column name (column[0]) - no matter what it is and using that to sort. But this seems kinda sketchy, no?

My question is, is there a better way to determine what 开发者_如何学运维to sort my DataView by when column names are unknown?

Thanks!


I haven't tried it, but since it sorts based on the column name you pass in, couldn't you sort based on index with something like:

dataview.Sort = dataview.Table.Columns[index].ColumnName + " DESC";

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜