- PR -

DataGridViewでの行移動の方法

1
投稿者投稿内容
next
会議室デビュー日: 2008/08/22
投稿数: 15
投稿日時: 2008-10-23 13:55
DataGridViewの外にあるボタン押下で、DataGridViewの任意行を1行上に移動させようと思っているのですが…
************************************************************
int idx = dg項目Editor.CurrentCell.RowIndex;
datatable.Rows.InsertAt(datatable.Rows[idx], idx+1);
datatable.Rows[idx].Delete();
************************************************************
上記コードを実装すると、2行目において「この行は既にこのテーブルに属しています。」というエラーが返されます。意味はそのままで同じ行を2つも入れるなと怒られていると思うんですが…この実装に悩んでいます。
お分かりの方ご教授お願いします。

King
ぬし
会議室デビュー日: 2008/06/20
投稿数: 284
投稿日時: 2008-10-23 15:56
1.移動させたい行を複製。
2.複製した行を移動先に挿入。
3.複製元の行を削除。

じゃだめですかね。
next
会議室デビュー日: 2008/08/22
投稿数: 15
投稿日時: 2008-10-24 10:40
返信ありがとうございます。
*******************************************************************
if (dg項目Editor.CurrentCell == null) return;
if (dg項目Editor.CurrentCell.RowIndex == 0 || dg項目Editor.CurrentCell.RowIndex == dg項目Editor.Rows.Count - 1) return;
object[] obj = datatable.Rows[dg項目Editor.CurrentCell.RowIndex].ItemArray;
object[] obj2 = datatable.Rows[dg項目Editor.CurrentCell.RowIndex - 1].ItemArray;
datatable.Rows[dg項目Editor.CurrentCell.RowIndex].ItemArray = obj2;
datatable.Rows[dg項目Editor.CurrentCell.RowIndex - 1].ItemArray = obj;
dg項目Editor.CurrentCell = dg項目Editor.Rows[dg項目Editor.CurrentCell.RowIndex - 1].Cells[0];
**************************************************************************
上記コードで無事実装する事が出来ました。
又、宜しくお願いします。
1

スキルアップ/キャリアアップ(JOB@IT)