1列目のソート結果を維持して2列目のソートを行いたい
対象製品
MultiRow for Windows Forms 10.0J
詳細
組み込みのソート機能では実現することができませんが、列ヘッダのクリックを検知して独自のソートを行うことで実現できます。
[Visual Basic]
[C#]
[Visual Basic]
Imports GrapeCity.Win.MultiRow Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim template1 As Template = Template.CreateGridTemplate(2) GcMultiRow1.Template = template1 GcMultiRow1.RowCount = 10 GcMultiRow1.AllowUserToAddRows = False Dim r As New Random() For i As Integer = 0 To GcMultiRow1.RowCount - 1 GcMultiRow1.SetValue(i, 0, r.[Next](1, 3)) GcMultiRow1.SetValue(i, 1, r.[Next](1, 100)) Next End Sub Private Sub GcMultiRow1_CellClick(ByVal sender As Object, ByVal e As CellEventArgs) Handles GcMultiRow1.CellClick ' 列ヘッダがクリックされた場合 If e.Scope = CellScope.ColumnHeader Then ' 対象となる列ヘッダの取得 Dim chCell As ColumnHeaderCell = GcMultiRow1.ColumnHeaders(0).Cells(e.CellIndex) ' ソートインジケータの設定 If chCell.SortGlyphDirection = SortOrder.Ascending Then chCell.SortGlyphDirection = SortOrder.Descending Else chCell.SortGlyphDirection = SortOrder.Ascending End If ' ソートの実行 Dim si0 As SortItem = New SortItem(0, SortOrder.None) Dim si1 As SortItem = New SortItem(1, SortOrder.None) Select Case e.CellIndex Case 0 si0.SortOrder = chCell.SortGlyphDirection Exit Select Case 1 si0.SortOrder = DirectCast(GcMultiRow1.ColumnHeaders(0).Cells(0), ColumnHeaderCell).SortGlyphDirection si1.SortOrder = chCell.SortGlyphDirection Exit Select End Select GcMultiRow1.Sort(New SortItem() {si0, si1}) End If End Sub
[C#]
using GrapeCity.Win.MultiRow; private void Form1_Load(object sender, EventArgs e) { Template template1 = Template.CreateGridTemplate(2); gcMultiRow1.Template = template1; gcMultiRow1.RowCount = 10; gcMultiRow1.AllowUserToAddRows = false; gcMultiRow1.CellClick += new EventHandler(gcMultiRow1_CellClick); Random r = new Random(); for (int i = 0; i < gcMultiRow1.RowCount; i++) { gcMultiRow1.SetValue(i, 0, r.Next(1, 3)); gcMultiRow1.SetValue(i, 1, r.Next(1, 100)); } } private void gcMultiRow1_CellClick(object sender, CellEventArgs e) { // 列ヘッダがクリックされた場合 if (e.Scope == CellScope.ColumnHeader) { // 対象となる列ヘッダの取得 ColumnHeaderCell chCell = (ColumnHeaderCell)gcMultiRow1.ColumnHeaders[0].Cells[e.CellIndex]; // ソートインジケータの設定 if (chCell.SortGlyphDirection == SortOrder.Ascending) { chCell.SortGlyphDirection = SortOrder.Descending; } else { chCell.SortGlyphDirection = SortOrder.Ascending; } // ソートの実行 SortItem si0 = new SortItem(0, SortOrder.None); SortItem si1 = new SortItem(1, SortOrder.None); switch (e.CellIndex) { case 0: si0.SortOrder = chCell.SortGlyphDirection; break; case 1: si0.SortOrder = ((ColumnHeaderCell)gcMultiRow1.ColumnHeaders[0].Cells[0]).SortGlyphDirection; si1.SortOrder = chCell.SortGlyphDirection; break; } gcMultiRow1.Sort(new SortItem[] { si0, si1 }); } }