作成日: 2020/04/14 最終更新日: 2020/04/14
文書種別
使用方法
詳細
FlexGridでは、データ連結していない場合は、Moveメソッドを使用して指定した場所に行を移動することができますが、データ連結しているときに行を移動する機能は備わっていません。
この場合の対応方法として、C1FlexGridのBeforeDragRowイベントで、既定の動作をキャンセルした上で、データソース側で該当するデータ行を入れ替える手段が考えられます。
以下に、連結グリッドで、選択した行をドラッグ&ドロップにて任意の場所に移動するサンプルを紹介します。
◎サンプルコード(VB)
この場合の対応方法として、C1FlexGridのBeforeDragRowイベントで、既定の動作をキャンセルした上で、データソース側で該当するデータ行を入れ替える手段が考えられます。
以下に、連結グリッドで、選択した行をドラッグ&ドロップにて任意の場所に移動するサンプルを紹介します。
◎サンプルコード(VB)
Imports C1.Win.C1FlexGrid
Public Class Form1
Private dt As DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' データの作成
'Dim dt As New DataTable("Test")
dt = New DataTable("Test")
dt.Columns.Add("ColA", GetType(String))
dt.Columns.Add("ColB", GetType(String))
For i = 0 To 4
dt.Rows.Add($"A{i}", $"B{i}")
Next
dt.AcceptChanges()
' FlexGridの設定
C1FlexGrid1.DataSource = dt
C1FlexGrid1.AllowDragging = AllowDraggingEnum.Rows
End Sub
Private Sub C1FlexGrid1_BeforeDragRow(sender As Object, e As DragRowColEventArgs) Handles C1FlexGrid1.BeforeDragRow
' 既定の動作のキャンセル
e.Cancel = True
' データソース側での行の入れ替え
Dim temp As DataRow = dt.NewRow()
Dim oldRow As Integer = e.Row - 1
Dim newRow As Integer = e.Position - 1
temp.ItemArray = dt.Rows(oldRow).ItemArray
dt.Rows.RemoveAt(oldRow)
dt.Rows.InsertAt(temp, newRow)
End Sub
End Class
◎サンプルコード(C#)using C1.Win.C1FlexGrid;
namespace prj_C1FlexGrid_move
{
public partial class Form1 : Form
{
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// データの作成
dt = new DataTable("Test");
dt.Columns.Add("ColA", typeof(string));
dt.Columns.Add("ColB", typeof(string));
for (int i = 0; i <= 4; i++)
{
dt.Rows.Add($"A{i}", $"B{i}");
}
dt.AcceptChanges();
// lexGridの設定
c1FlexGrid1.DataSource = dt;
c1FlexGrid1.AllowDragging = AllowDraggingEnum.Rows;
}
private void c1FlexGrid1_BeforeDragRow(object sender, C1.Win.C1FlexGrid.DragRowColEventArgs e)
{
// 既定の動作のキャンセル
e.Cancel = true;
// データソース側での行の入れ替え
DataRow temp = dt.NewRow();
int oldRow = (e.Row - 1);
int newRow = (e.Position - 1);
temp.ItemArray = dt.Rows[oldRow].ItemArray;
dt.Rows.RemoveAt(oldRow);
dt.Rows.InsertAt(temp, newRow);
}
}
}
旧文書番号
85477