MESCIUS SPREAD for Windows Forms 17.0J サンプルコード集
コードからテーブルのソートの実行

TableViewクラスのSortメソッドにより、任意のタイミング(ボタンクリックイベントなど)でテーブルのソートを実行させることができます。

【実行例】

  • テーブルのソート機能では、表示上のデータではなく、実データの並び順(データモデル上の並び順)が変更されます。
  • ソート実行後に元の状態に戻すことはできません。
  • 昇順/降順共に未入力セル(null/Nothing)は最後にソートされます。String.Emptyは他の文字列と同時にソートされます。

private void Form1_Load(object sender, EventArgs e)
{
    // テストデータの設定
    fpSpread1.ActiveSheet.Cells[0, 0].Value = "インデックス";
    fpSpread1.ActiveSheet.Cells[1, 0].Value = 1;
    fpSpread1.ActiveSheet.Cells[2, 0].Value = 2;
    fpSpread1.ActiveSheet.Cells[3, 0].Value = 3;
    fpSpread1.ActiveSheet.Cells[4, 0].Value = 4;
    fpSpread1.ActiveSheet.Cells[5, 0].Value = 5;
    fpSpread1.ActiveSheet.Cells[0, 1].Value = "果物";
    fpSpread1.ActiveSheet.Cells[1, 1].Value = "りんご";
    fpSpread1.ActiveSheet.Cells[2, 1].Value = "バナナ";
    fpSpread1.ActiveSheet.Cells[3, 1].Value = "みかん";
    fpSpread1.ActiveSheet.Cells[4, 1].Value = "なし";
    fpSpread1.ActiveSheet.Cells[5, 1].Value = "いちご";
    fpSpread1.ActiveSheet.Cells[0, 2].Value = "個数";
    fpSpread1.ActiveSheet.Cells[1, 2].Value = 10;
    fpSpread1.ActiveSheet.Cells[2, 2].Value = 50;
    fpSpread1.ActiveSheet.Cells[3, 2].Value = 10;
    fpSpread1.ActiveSheet.Cells[4, 2].Value = 50;
    fpSpread1.ActiveSheet.Cells[5, 2].Value = 10;
    fpSpread1.ActiveSheet.Columns[0].Width = 100;
    // テーブルの追加
    fpSpread1.ActiveSheet.AddTable("table1", 0, 0, 6, 3);
}
 
private void button1_Click(object sender, EventArgs e)
{
    // ソートの実行
    FarPoint.Win.Spread.TableView table = fpSpread1.ActiveSheet.GetTable("table1");
    FarPoint.Win.Spread.ComplexSortInfo[] sort = new FarPoint.Win.Spread.ComplexSortInfo[2];
    sort[0] = new FarPoint.Win.Spread.ComplexSortInfo(2, true);
    sort[1] = new FarPoint.Win.Spread.ComplexSortInfo(0, true);
    table.Sort(sort);
}
Private Sub Table_03_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ' テストデータの設定
    FpSpread1.ActiveSheet.Cells(0, 0).Value = "インデックス"
    FpSpread1.ActiveSheet.Cells(1, 0).Value = 1
    FpSpread1.ActiveSheet.Cells(2, 0).Value = 2
    FpSpread1.ActiveSheet.Cells(3, 0).Value = 3
    FpSpread1.ActiveSheet.Cells(4, 0).Value = 4
    FpSpread1.ActiveSheet.Cells(5, 0).Value = 5
    FpSpread1.ActiveSheet.Cells(0, 1).Value = "果物"
    FpSpread1.ActiveSheet.Cells(1, 1).Value = "りんご"
    FpSpread1.ActiveSheet.Cells(2, 1).Value = "バナナ"
    FpSpread1.ActiveSheet.Cells(3, 1).Value = "みかん"
    FpSpread1.ActiveSheet.Cells(4, 1).Value = "なし"
    FpSpread1.ActiveSheet.Cells(5, 1).Value = "いちご"
    FpSpread1.ActiveSheet.Cells(0, 2).Value = "個数"
    FpSpread1.ActiveSheet.Cells(1, 2).Value = 10
    FpSpread1.ActiveSheet.Cells(2, 2).Value = 50
    FpSpread1.ActiveSheet.Cells(3, 2).Value = 10
    FpSpread1.ActiveSheet.Cells(4, 2).Value = 50
    FpSpread1.ActiveSheet.Cells(5, 2).Value = 10
    FpSpread1.ActiveSheet.Columns(0).Width = 100
    ' テーブルの追加
    FpSpread1.ActiveSheet.AddTable("table1", 0, 0, 6, 3)
End Sub
 
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ' ソートの実行
    Dim table As FarPoint.Win.Spread.TableView = FpSpread1.ActiveSheet.GetTable("table1")
    Dim sort As FarPoint.Win.Spread.ComplexSortInfo() = New FarPoint.Win.Spread.ComplexSortInfo(1) {}
    sort(0) = New FarPoint.Win.Spread.ComplexSortInfo(2, True)
    sort(1) = New FarPoint.Win.Spread.ComplexSortInfo(0, True)
    table.Sort(sort)
End Sub

 

 


© MESCIUS inc. All rights reserved.