ソート時にNothingの項目を除外する方法

文書番号 : 83287     文書種別 : 使用方法     登録日 : 2018/10/11     最終更新日 : 2018/10/11
文書を印刷する
対象製品
MultiRow for Windows Forms 10.0J
詳細
ColumnHeaderCellのSortModeプロパティをProgrammaticに設定してCellMouseClickイベントで独自に実装を行ってNothingを除外したソートを実行することが可能です。

[Visual Basic]
Imports GrapeCity.Win.MultiRow
Public Class Form1
    Private sortComparer As New WithoutNullComparer()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' テンプレートの作成
        Dim template1 As Template = Template.CreateGridTemplate(2)
        DirectCast(template1.ColumnHeaders(0).Cells(0), ColumnHeaderCell).SortMode = SortMode.Programmatic

        ' MultiRowの設定
        GcMultiRow1.Template = template1
        GcMultiRow1.AllowUserToAddRows = False
        GcMultiRow1.RowCount = 5

        ' テスト用データの設定
        Dim r As New Random()
        For i As Integer = 0 To GcMultiRow1.RowCount - 1
            If i <> 2 Then
                GcMultiRow1.SetValue(i, 0, r.Next(1, 100))
            End If
            GcMultiRow1.SetValue(i, 1, String.Format("B{0}", i))
        Next
    End Sub

    Private Sub GcMultiRow1_CellMouseClick(sender As Object, e As CellMouseEventArgs) Handles GcMultiRow1.CellMouseClick
        ' 独自のソートの実装
        If e.CellName = "columnHeaderCell1" Then
            Dim grid As GcMultiRow = sender
            Dim chCell As ColumnHeaderCell = grid.ColumnHeaders(0).Cells(0)
            Dim sort As SortOrder
            If chCell.SortGlyphDirection = SortOrder.Ascending Then
                ' 降順のソート
                sort = SortOrder.Descending
                chCell.SortGlyphDirection = SortOrder.Descending
            Else
                ' 昇順のソート
                sort = SortOrder.Ascending
                chCell.SortGlyphDirection = SortOrder.Ascending
            End If
            grid.Sort("textBoxCell1", sort, sortComparer)
        End If
    End Sub
End Class
Public Class WithoutNullComparer
    Implements System.Collections.IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        If x = y Then
            Return 0
        End If
        If x Is Nothing Then
            Return Int32.MaxValue
        End If
        If y Is Nothing Then
            Return Int32.MinValue
        End If

        Dim ix As Integer = DirectCast(x, Integer)
        Dim iy As Integer = DirectCast(y, Integer)
        Return ix - iy
    End Function
End Class

[C#]
using GrapeCity.Win.MultiRow;

namespace sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        WithoutNullComparer sortComparer = new WithoutNullComparer();
        private void Form1_Load(object sender, EventArgs e)
        {
            // テンプレートの作成
            Template template1 = Template.CreateGridTemplate(2);
            ColumnHeaderCell columnHeaderCell = (ColumnHeaderCell)template1.ColumnHeaders[0].Cells[0];
            columnHeaderCell.SortMode = SortMode.Programmatic;

            // MultiRowの設定
            gcMultiRow1.Template = template1;
            gcMultiRow1.AllowUserToAddRows = false;
            gcMultiRow1.RowCount = 5;

            // テスト用データの設定
            Random r = new Random();
            for(int i = 0; i < gcMultiRow1.RowCount; i++)
            {
                if (i != 2)
                {
                    gcMultiRow1.SetValue(i, 0, r.Next(1, 100));
                }
                gcMultiRow1.SetValue(i, 1, String.Format("B{0}", i));
            }
        }

        private void gcMultiRow1_CellMouseClick(object sender, CellMouseEventArgs e)
        {
            // 独自ソートの実装
            if(e.CellName== "columnHeaderCell1")
            {
                GcMultiRow grid = (GcMultiRow)sender;
                ColumnHeaderCell chCell = (ColumnHeaderCell)grid.ColumnHeaders[0].Cells[0];
                SortOrder sort;

                if(chCell.SortGlyphDirection== SortOrder.Ascending)
                {
                    // 降順のソート
                    sort = SortOrder.Descending;
                    chCell.SortGlyphDirection = SortOrder.Descending;
                }else
                {
                    // 昇順のソート
                    sort = SortOrder.Ascending;
                    chCell.SortGlyphDirection = SortOrder.Ascending;

                }
                grid.Sort("textBoxCell1", sort, sortComparer);
            }
        }
    }
    public class WithoutNullComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            if (x == y)
            {
                return 0;
            }

            if(x == null){
                return Int32.MaxValue;
            }

            if (y == null)
            {
                return Int32.MinValue;
            }

            int ix = (int)x;
            int iy = (int)y;
            return ix - iy;
        }
    }
}