首页 > 解决方案 > 除了满足条件的行,如何对 DataTable 进行排序?

问题描述

我试图DataTable在 C# 中按特定顺序对 a 进行排序,它的特殊之处在于,如果满足给定条件,我需要一些行保留在原始位置。

我目前正在使用这个功能:

OriginalTable.DefaultView.Sort = "[column_name] ASC";

包含没有数据的OriginalTable行,我需要保留这些行,而其他行按 ASC 条件排序。任何可能有助于解决这个问题的想法?

提前致谢!。

标签: c#sortingdatatable

解决方案


我不知道结构。而且我确信没有直接的方法可以满足您的要求。只是想在这里给出指针/伪代码。

Step 1: Create a dictionary(Key-value) for the rows which met your condition. Ensure to keep the index of the item in the key
Step 2: Create a list with rows which do not met your condition. Then sort it
Step 3: Insert the rows from step 2 into your datatable one by one checking the index. If you find the index in step 1 rows, insert it at that location

我希望它有帮助


推荐阅读