0
私は自分のページのgridViewにバインドされたdataTableを使って作業しています。ユーザーは、変更が行われるたびに各行がソートされるように、gridView行を削除および変更できます。 gridViewの背後にあるdataTableは、2つのフィールドの昇順でソートされます。あるグループ内の各要素を数え、もう1つはグループ索引をソートする要素です。更新後のデータテーブルの並べ替え
ユーザーはグループを削除できますが、番号の順序は間違っています。私は必要なの再インデックスする表Iは、隙間なく連続番号を持っているように、グループが削除されます毎回:例えば
:私はこの機能を試してみました
group id group id sorted
0 0
0 0
0 0
1 1
1 1
3 2
3 2
6 3
6 3
が、何もありませんwiht(I期待している)。誰かがすでにこの問題をすでに解決しており、助けてくれるかもしれません。
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;
}