作成日: 2019/11/08 最終更新日: 2019/11/08
文書種別
使用方法
詳細
マウスやコードを使用してセル範囲を選択した場合、範囲内のデータだけをソートするための直接的な方法は提供されておりません。
代替方法として、作業用のFlexGridを用意し、選択したセル範囲のデータをここにコピーしてソートを実行した後、元の選択範囲にデータを戻す手段が考えられます。
新規フォームにC1FlexGridとButtonコントロールを配置して以下のコードを記載後、実行し、2列目(B列)の任意範囲を選択してボタンをクリックし、動作をご確認ください。
◎サンプルコード(VB)
◎サンプルコード(C#)
代替方法として、作業用のFlexGridを用意し、選択したセル範囲のデータをここにコピーしてソートを実行した後、元の選択範囲にデータを戻す手段が考えられます。
新規フォームにC1FlexGridとButtonコントロールを配置して以下のコードを記載後、実行し、2列目(B列)の任意範囲を選択してボタンをクリックし、動作をご確認ください。
◎サンプルコード(VB)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' テストデータの作成
Dim dt As DataTable = New DataTable("Test")
dt.Columns.Add("A", GetType(Int32))
dt.Columns.Add("B", GetType(Int32))
dt.Columns.Add("C", GetType(String))
Dim r As New Random()
For i As Integer = 0 To 10
dt.Rows.Add(i + 1, r.Next(10, 100))
If i Mod 2 = 1 Then
dt.Rows(i)("C") = "Blue"
End If
Next
dt.AcceptChanges()
C1FlexGrid1.DataSource = dt
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
' 選択したセル範囲を取得
Dim cr As C1.Win.C1FlexGrid.CellRange = Me.C1FlexGrid1.Selection
' 作業用のグリッドを作成
Dim fg As C1.Win.C1FlexGrid.C1FlexGrid = New C1.Win.C1FlexGrid.C1FlexGrid()
' 選択範囲の行数・列数を作業用グリッドの行・列の数として設定
fg.Cols.Count = cr.c2 - cr.c1 + 1
fg.Cols.Fixed = 0
fg.Rows.Count = cr.r2 - cr.r1 + 1
fg.Rows.Fixed = 0
Dim _row As Integer = 0
Dim _col As Integer = 0
' 選択範囲のデータを作業用グリッドに入力
'Dim i As Integer = cr.r1
'Dim j As Integer = cr.r1
Dim i, j As Integer
For i = cr.r1 To cr.r2
For j = cr.c1 To cr.c2
fg(_row, _col) = Me.C1FlexGrid1(i, j)
_col = _col + 1
Next
_row = _row + 1
_col = 0
Next
' ソートタイプとソートする列番号(例:0)を指定してソートを実行
fg.Sort(C1.Win.C1FlexGrid.SortFlags.Ascending, 0)
_row = 0
_col = 0
' 作業用グリッドのデータを元グリッドの選択範囲に設定
For i = cr.r1 To cr.r2
For j = cr.c1 To cr.c2
Me.C1FlexGrid1(i, j) = fg(_row, _col)
_col = _col + 1
Next
_row = _row + 1
_col = 0
Next
' 作業用グリッドを解放
fg.Dispose
End Sub
◎サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
// テストデータの作成
DataTable dt = new DataTable("Test");
dt.Columns.Add("A", typeof(Int32));
dt.Columns.Add("B", typeof(Int32));
dt.Columns.Add("C", typeof(string));
Random r = new Random();
for (int i = 0; (i <= 10); i++)
{
dt.Rows.Add((i + 1), r.Next(10, 100));
if (((i % 2)== 1))
{
dt.Rows[i]["C"] = "Blue";
}
}
dt.AcceptChanges();
c1FlexGrid1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
// 選択したセル範囲を取得
C1.Win.C1FlexGrid.CellRange cr = this.c1FlexGrid1.Selection;
// 作業用のグリッドを作成
var fg = new C1.Win.C1FlexGrid.C1FlexGrid();
// 選択範囲の行数・列数を作業用グリッドの行・列の数として設定
fg.Cols.Count = cr.c2 - cr.c1 + 1;
fg.Cols.Fixed = 0;
fg.Rows.Count = cr.r2 - cr.r1 + 1;
fg.Rows.Fixed = 0;
int _row = 0;
int _col = 0;
// 選択範囲のデータを作業用グリッドに入力
for (int i = cr.r1; i <= cr.r2; i++)
{
for (int j = cr.c1; j <= cr.c2; j++)
{
fg[_row, _col] = this.c1FlexGrid1[i, j];
_col += 1;
}
_row += 1;
_col = 0;
}
// ソートタイプとソートする列番号(例:0)を指定してソートを実行
fg.Sort(C1.Win.C1FlexGrid.SortFlags.Ascending, 0);
_row = 0;
_col = 0;
// 作業用グリッドのデータを元グリッドの選択範囲に設定
for (int i = cr.r1; i <= cr.r2; i++)
{
for (int j = cr.c1; j <= cr.c2; j++)
{
this.c1FlexGrid1[i, j] = fg[_row, _col];
_col += 1;
}
_row += 1;
_col = 0;
}
// 作業用グリッドを解放
fg.Dispose();
}
旧文書番号
84621