简体   繁体   English

在 ASP.NET C# 中重新排序多级数

[英]Reordering Multilevel Number in ASP.NET C#

DataTable columns selected from SQL as following:从 SQL 中选择的 DataTable 列如下:

id ID level_0 level_0 level_1 1级 level_2 level_2 description描述
114 114 1 1 0 0 0 0 description 1说明 1
115 115 1 1 1 1 0 0 description 2说明 2
116 116 1 1 1 1 1 1 description 3说明 3
117 117 1 1 1 1 2 2 description 4说明 4

and so on...等等...

A list of multilevel number displayed in the user interface as following from datatable用户界面中显示的多级数字列表,如下所示来自数据表

1.0.0  description 1
1.1.0  description 2
1.1.1  description 3
1.1.2  description 4
1.1.3  description 5
1.2.0  description 6
1.2.1  description 7
1.2.2  description 8
1.2.3  description 9
1.2.4  description 10
1.3.0  description 11
1.3.1  description 12
2.0.0  description 13
2.1.0  description 14
2.2.0  description 15
3.0.0  description 16
3.1.0  description 17
... and so on

If the user deleting these items: 1.1.2, 1.2.2, 2.0.0, 2.1.0, 2.2.0, my current result will be:如果用户删除这些项目:1.1.2、1.2.2、2.0.0、2.1.0、2.2.0,我当前的结果将是:

1.0.0  description 1
1.1.0  description 2
1.1.1  description 3
1.1.3  description 5
1.2.0  description 6
1.2.1  description 7
1.2.3  description 9
1.2.4  description 10
1.3.0  description 11
1.3.1  description 12
3.0.0  description 16
3.1.0  description 17

Expected output:预期输出:

1.0.0  description 1
1.1.0  description 2
1.1.1  description 3
1.1.2  description 5
1.2.0  description 6
1.2.1  description 7
1.2.2  description 9
1.2.3  description 10
1.3.0  description 11
1.3.1  description 12
2.0.0  description 16
2.1.0  description 17

May I know, how to write the code behind ASP.NET C# or VB.NET code to reordering my multi-level numbering after user deleted some wrong items, by updating my data table multilevel number to the correct one in the expected output?我可以知道,如何编写 ASP.NET C# 或 VB.NET 代码背后的代码,以便在用户删除一些错误的项目后重新排序我的多级编号,方法是将我的数据表多级编号更新为预期输出中的正确编号?

Assuming you don't mind being tied to having 3 levels in your code, you just need to setup a 3 level counter and then reset each level when the original level changes to renumber every item:假设您不介意代码中有 3 个级别,您只需要设置一个 3 级计数器,然后在原始级别更改时重置每个级别以重新编号每个项目:

int level0 = 1, level1 = 0, level2 = 0;
var firstRow = dt.Rows[0];
int curLevel0 = firstRow.Field<int>("level_0"), curLevel1 = firstRow.Field<int>("level_1");
foreach (var row in dt.AsEnumerable()) {
    if (row.Field<int>("level_0") != curLevel0) {
        curLevel0 = row.Field<int>("level_0");
        curLevel1 = row.Field<int>("level_1");
        ++level0;
        level1 = 0;
        level2 = 0;
    }
    if (row.Field<int>("level_0") != level0)
        row["level_0"] = level0;

    if (row.Field<int>("level_1") != curLevel1) {
        curLevel1 = row.Field<int>("level_1");
        ++level1;
        level2 = 0;
    }
    if (row.Field<int>("level_1") != level1)
        row["level_1"] = level1;

    if (row.Field<int>("level_2") != level2)
        row["level_2"] = level2;
    ++level2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM