更新后对数据表进行排序

时间:2016-05-03 10:15:40

标签: c# asp.net sorting gridview datatable

我正在使用绑定到我页面上的gridView的dataTable。用户可以删除和修改gridView行,以便在每次更改后对每行进行排序。 gridView后面的dataTable按两个字段的升序排序。一个计算组中的每个元素,另一个计算组索引。

用户可以删除组,但数字的顺序则会搞砸。我需要的是每次删除一组时重新索引表,以便我有序列号没有间隙:

例如:

group id    group id sorted
0           0
0           0 
0           0
1           1
1           1
3           2
3           2
6           3
6           3

我尝试了这个功能,但没有结果(我预料到的)。也许某人之前已经解决了这个问题,可以提供帮助。

 protected void reindexGroupsLayers(DataTable dtLayers)
{
    //after every change to the index structure 
    //this function re-sorts the index
    DataView uniqueGroupIndexesView = new DataView(dtLayers);
    uniqueGroupIndexesView.Sort = "groupindex asc";
    DataTable uniqueGroupIndexesTable = uniqueGroupIndexesView.ToTable(true, "groupindex");
    int i = 0;
    foreach (DataRow uniqueRow in uniqueGroupIndexesTable.Rows)
    {
        int oldIndex = (int)uniqueRow["groupindex"];
        foreach (DataRow groupRow in dtLayers.Rows)
        {
            if ((int)groupRow["groupindex"] == oldIndex)
            {
                groupRow["groupindex"] = i;
            }
            else
            {
                continue;
            }
            i++;
        }
    }
    DataView dtLayersView = dtLayers.DefaultView;
    dtLayersView.Sort = "groupindex, layerindex asc";
    DataTable dtLayersSorted = dtLayersView.ToTable();
    gvMapLayers.DataSource = dtLayersSorted;
    gvMapLayers.DataBind();
    Session["webmaplayers"] = dtLayersSorted;
}

1 个答案:

答案 0 :(得分:-1)

您可以使用" DataTable"这个问题的JavaScript功能。请参考https://www.datatables.net/

它将使用javascript中的seting参数设置排序问题。

如果您需要进一步的帮助,请告诉我。

谢谢