【发布时间】:2016-08-28 08:08:51
【问题描述】:
我正在使用绑定到我页面上的 gridView 的数据表。用户可以删除和修改 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;
}
【问题讨论】:
标签: c# asp.net sorting gridview datatable