作成日: 2026/06/24 最終更新日: 2026/06/24
文書種別
使用方法
詳細
行や列のドラッグ移動を許可した場合、特定の条件で移動をキャンセルすることはできませんが、移動後に発生するRowCollectionChanged、ColumnCollectionChangedイベントを使用して移動前の状態に戻すことはできます。
◎サンプルコード(VB)
◎サンプルコード(C#)
◎サンプルコード(VB)
Private mvrFLG As Boolean Private mvcFLG As Boolean Public Sub New() InitializeComponent() ' 行列移動を許可 GcSpreadGrid1.RowDragMode = GrapeCity.Windows.SpreadGrid.DragMode.SelectThenDrag GcSpreadGrid1.ColumnDragMode = GrapeCity.Windows.SpreadGrid.DragMode.SelectThenDrag mvrFLG = False mvcFLG = False ' 固定行列の設定 GcSpreadGrid1.FrozenRowCount = 3 GcSpreadGrid1.FrozenColumnCount = 3 ' テストデータの設定 For i As Integer = 0 To GcSpreadGrid1.RowCount - 1 For j As Integer = 0 To GcSpreadGrid1.ColumnCount - 1 GcSpreadGrid1.Cells(i, j).Value = "R=" + i.ToString() + "C=" + j.ToString() Next Next End Sub ' 固定行の移動を禁止 Private Sub GcSpreadGrid1_RowCollectionChanged(sender As Object, e As GrapeCity.Windows.SpreadGrid.SpreadCollectionChangedEventArgs) Handles GcSpreadGrid1.RowCollectionChanged If Not mvrFLG Then mvrFLG = True If e.OldStartingIndex < GcSpreadGrid1.FrozenRowCount OrElse e.NewStartingIndex < GcSpreadGrid1.FrozenRowCount Then GcSpreadGrid1.Rows.Move(e.NewStartingIndex, e.OldStartingIndex, e.Count) End If mvrFLG = False End If End Sub ' 固定列の移動を禁止 Private Sub GcSpreadGrid1_ColumnCollectionChanged(sender As Object, e As GrapeCity.Windows.SpreadGrid.SpreadCollectionChangedEventArgs) Handles GcSpreadGrid1.ColumnCollectionChanged If Not mvcFLG Then mvcFLG = True If e.OldStartingIndex < GcSpreadGrid1.FrozenColumnCount OrElse e.NewStartingIndex < GcSpreadGrid1.FrozenColumnCount Then GcSpreadGrid1.Columns.Move(e.NewStartingIndex, e.OldStartingIndex) End If mvcFLG = False End If End Sub
◎サンプルコード(C#)
private bool mvrFLG;
private bool mvcFLG;
public MainWindow()
{
InitializeComponent();
// 行列移動を許可
gcSpreadGrid1.RowDragMode = GrapeCity.Windows.SpreadGrid.DragMode.SelectThenDrag;
gcSpreadGrid1.ColumnDragMode = GrapeCity.Windows.SpreadGrid.DragMode.SelectThenDrag;
mvrFLG = false;
mvcFLG = false;
// 固定行列の設定
gcSpreadGrid1.FrozenRowCount = 3;
gcSpreadGrid1.FrozenColumnCount = 3;
// テストデータの設定
for (int i = 0; i <= gcSpreadGrid1.RowCount - 1; i++)
{
for (int j = 0; j <= gcSpreadGrid1.ColumnCount - 1; j++)
{
gcSpreadGrid1.Cells[i, j].Value = "R=" + i.ToString() + "C=" + j.ToString();
}
}
// イベントの関連付け
gcSpreadGrid1.RowCollectionChanged += new EventHandler(gcSpreadGrid1_RowCollectionChanged);
gcSpreadGrid1.ColumnCollectionChanged += new EventHandler(gcSpreadGrid1_ColumnCollectionChanged);
}
// 固定行の移動を禁止
void gcSpreadGrid1_RowCollectionChanged(object sender, GrapeCity.Windows.SpreadGrid.SpreadCollectionChangedEventArgs e)
{
if (!mvrFLG)
{
mvrFLG = true;
if (e.OldStartingIndex < gcSpreadGrid1.FrozenRowCount || e.NewStartingIndex < gcSpreadGrid1.FrozenRowCount)
{
gcSpreadGrid1.Rows.Move(e.NewStartingIndex, e.OldStartingIndex, e.Count);
}
mvrFLG = false;
}
}
// 固定列の移動を禁止
void gcSpreadGrid1_ColumnCollectionChanged(object sender, GrapeCity.Windows.SpreadGrid.SpreadCollectionChangedEventArgs e)
{
if (!mvcFLG)
{
mvcFLG = true;
if (e.OldStartingIndex < gcSpreadGrid1.FrozenColumnCount || e.NewStartingIndex < gcSpreadGrid1.FrozenColumnCount)
{
gcSpreadGrid1.Columns.Move(e.NewStartingIndex, e.OldStartingIndex);
}
mvcFLG = false;
}
}