列ヘッダ型セル(ColumnHeaderCell)のフィルタでチェックされているアイテムを確認するには?

文書番号 : 40566     文書種別 : 使用方法     登録日 : 2017/07/26     最終更新日 : 2017/07/26
文書を印刷する
対象製品
MultiRow for Windows Forms 10.0J
詳細
列ヘッダ型セル(ColumnHeaderCell)のドロップダウンボタンを使用した組み込みフィルタ(ColumnHeaderCell.DropDownList)で、ユーザーによって選択されたフィルタを確認するには次のようにコーディングします。

[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)
    Dim columnHeaderCell1 As ColumnHeaderCell = TryCast(template1.ColumnHeaders(0).Cells(0), ColumnHeaderCell)
    columnHeaderCell1.DropDownList = New HeaderDropDownList("textBoxCell1", True, False)
    GcMultiRow1.Template = template1
    GcMultiRow1.RowCount = 10

    For i As Integer = 0 To GcMultiRow1.RowCount - 1
        GcMultiRow1.SetValue(i, 0, i Mod 3)
        GcMultiRow1.SetValue(i, 1, String.Format("R{0}C1", i))
    Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim columnHeaderCell1 As ColumnHeaderCell = TryCast(GcMultiRow1.ColumnHeaders(0).Cells(0), ColumnHeaderCell)
    For Each item As DropDownItem In columnHeaderCell1.DropDownList.Items
        Console.WriteLine("{0} : {1} = {2}", item.Text, item.GetType().Name, item.Checked)
    Next
End Sub

[C#]
using GrapeCity.Win.MultiRow;

private void Form1_Load(object sender, EventArgs e)
{
    Template template1 = Template.CreateGridTemplate(2);
    ColumnHeaderCell columnHeaderCell1 = template1.ColumnHeaders[0].Cells[0] as ColumnHeaderCell;
    columnHeaderCell1.DropDownList = new HeaderDropDownList("textBoxCell1", true, false);
    gcMultiRow1.Template = template1;
    gcMultiRow1.RowCount = 10;
    for (int i = 0; i < gcMultiRow1.RowCount; i++)
    {
        gcMultiRow1.SetValue(i, 0, i % 3);
        gcMultiRow1.SetValue(i, 1, String.Format("R{0}C1", i));
    } 
}

private void button1_Click(object sender, EventArgs e)
{
    ColumnHeaderCell columnHeaderCell1 = gcMultiRow1.ColumnHeaders[0].Cells[0] as ColumnHeaderCell;
    foreach (DropDownItem item in columnHeaderCell1.DropDownList.Items)
    {
        Console.WriteLine("{0} : {1} = {2}", item.Text, item.GetType().Name, item.Checked);
    }
}

ただし、上記のコードでは自動生成されたアイテム(DropDownAutoFilterItem)に含まれる個別のアイテムの選択状態を取得することはできません。DropDownAutoFilterItemはグリッドのセルに含まれる値を元にフィルタのアイテムを自動生成しますが、DropDownAutoFilterItem自体は1つのアイテムとして実装されているためです。

DropDownAutoFilterItemでどのアイテムが選択されているかを確認するには、選択されたアイテムの情報を手動で管理する必要があります。

[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)
    Dim columnHeaderCell1 As ColumnHeaderCell = TryCast(template1.ColumnHeaders(0).Cells(0), ColumnHeaderCell)
    columnHeaderCell1.DropDownList = New HeaderDropDownList("textBoxCell1", True, False)
    AddHandler TryCast(columnHeaderCell1.DropDownList.Items(3), DropDownAutoFilterItem).DropDownItemNeeded, AddressOf Me.DropDownAutoFilterItem_DropDownItemNeeded
    AddHandler columnHeaderCell1.DropDownList.Closed, AddressOf Me.DropDownList_Closed

    For i As Integer = 0 To columnHeaderCell1.DropDownList.Items.Count - 1
        AddHandler columnHeaderCell1.DropDownList.Items(i).Click, AddressOf Me.DropDownItem_Click
    Next

    GcMultiRow1.Template = template1
    GcMultiRow1.RowCount = 10

    For i As Integer = 0 To GcMultiRow1.RowCount - 1
        GcMultiRow1.SetValue(i, 0, i Mod 3)
        GcMultiRow1.SetValue(i, 1, String.Format("R{0}C1", i))
    Next
End Sub

Private _autoFilterItems As List(Of DropDownCustomFilterItem) = New List(Of DropDownCustomFilterItem)()
Private _autoFilterText As String = ""
Private _autoFilterChecked As Boolean = False

Private Sub DropDownAutoFilterItem_DropDownItemNeeded(ByVal sender As System.Object, ByVal e As DropDownItemNeededEventArgs)
    ' DropDownAutoFilterItemが自動的に追加するアイテムの代わりに
    ' 手動で作成したアイテムを追加します。

    Dim dropDownCustomFilterItem1 As New DropDownCustomFilterItem(e.FilterValue)
    AddHandler dropDownCustomFilterItem1.Click, AddressOf Me.DropDownCustomFilterItem_Click
    e.DropDownCustomFilterItem = dropDownCustomFilterItem1
    _autoFilterItems.Add(dropDownCustomFilterItem1)
    e.Handled = True
End Sub

Private Sub DropDownCustomFilterItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' DropDownAutoFilterItemで選択されたアイテムの情報を格納します。

    Dim item As DropDownItem = TryCast(sender, DropDownItem)
    _autoFilterText = item.Text
    _autoFilterChecked = True
End Sub

Private Sub DropDownItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' DropDownAutoFilterItem以外のアイテムが選択されたとき、
    ' DropDownAutoFilterItemの状態を初期化します。
    _autoFilterText = String.Empty
    _autoFilterChecked = False
End Sub

Private Sub DropDownList_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs)
    For i As Integer = 0 To _autoFilterItems.Count - 1
        RemoveHandler _autoFilterItems(i).Click, AddressOf Me.DropDownCustomFilterItem_Click
    Next
    _autoFilterItems.Clear()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim columnHeaderCell1 As ColumnHeaderCell = TryCast(GcMultiRow1.ColumnHeaders(0).Cells(0), ColumnHeaderCell)
    For Each item As DropDownItem In columnHeaderCell1.DropDownList.Items
        If TypeOf item Is DropDownAutoFilterItem Then
            Console.WriteLine("{0} : {1} = {2}", _autoFilterText, item.GetType().Name, _autoFilterChecked)
        Else
            Console.WriteLine("{0} : {1} = {2}", item.Text, item.GetType().Name, item.Checked)
        End If
    Next
End Sub

[C#]
using GrapeCity.Win.MultiRow;

private void Form1_Load(object sender, EventArgs e)
{
    Template template1 = Template.CreateGridTemplate(2);
    ColumnHeaderCell columnHeaderCell1 = template1.ColumnHeaders[0].Cells[0] as ColumnHeaderCell;
    columnHeaderCell1.DropDownList = new HeaderDropDownList("textBoxCell1", true, false);

    (columnHeaderCell1.DropDownList.Items[3] as DropDownAutoFilterItem).DropDownItemNeeded += new EventHandler<DropDownItemNeededEventArgs>(this.DropDownAutoFilterItem_DropDownItemNeeded);
    columnHeaderCell1.DropDownList.Closed += new EventHandler(DropDownList_Closed);

    for (int i = 0; i < columnHeaderCell1.DropDownList.Items.Count; i++)
    {
        columnHeaderCell1.DropDownList.Items[i].Click += new EventHandler(DropDownItem_Click);
    }

    gcMultiRow1.Template = template1;
    gcMultiRow1.RowCount = 10;
    for (int i = 0; i < gcMultiRow1.RowCount; i++)
    {
        gcMultiRow1.SetValue(i, 0, i % 3);
        gcMultiRow1.SetValue(i, 1, String.Format("R{0}C1", i));
    } 
}

private List<DropDownCustomFilterItem> _autoFilterItems = new List<DropDownCustomFilterItem>();
private string _autoFilterText = "";
private bool _autoFilterChecked = false;

private void DropDownAutoFilterItem_DropDownItemNeeded(object sender, DropDownItemNeededEventArgs e)
{
    // DropDownAutoFilterItemが自動的に追加するアイテムの代わりに
    // 手動で作成したアイテムを追加します。

    DropDownCustomFilterItem dropDownCustomFilterItem1 = new DropDownCustomFilterItem(e.FilterValue);
    dropDownCustomFilterItem1.Click += new EventHandler(this.DropDownCustomFilterItem_Click);
    e.DropDownCustomFilterItem = dropDownCustomFilterItem1;
    _autoFilterItems.Add(dropDownCustomFilterItem1);

    e.Handled = true;
}

private void DropDownCustomFilterItem_Click(object sender, EventArgs e)
{
    // DropDownAutoFilterItemで選択されたアイテムの情報を格納します。

    DropDownItem item = sender as DropDownItem;
    _autoFilterText = item.Text;
    _autoFilterChecked = true;
}

private void DropDownItem_Click(object sender, EventArgs e)
{
    // DropDownAutoFilterItem以外のアイテムが選択されたとき、
    // DropDownAutoFilterItemの状態を初期化します。

    _autoFilterText = string.Empty;
    _autoFilterChecked = false;
}

private void DropDownList_Closed(object sender, EventArgs e)
{
    for (int i = 0; i < _autoFilterItems.Count; i++)
    {
        _autoFilterItems[i].Click -= new EventHandler(this.DropDownCustomFilterItem_Click);
    }
    _autoFilterItems.Clear();
}

private void button1_Click(object sender, EventArgs e)
{
    ColumnHeaderCell columnHeaderCell1 = gcMultiRow1.ColumnHeaders[0].Cells[0] as ColumnHeaderCell;
    foreach (DropDownItem item in columnHeaderCell1.DropDownList.Items)
    {
        if (item is DropDownAutoFilterItem)
        {
            Console.WriteLine("{0} : {1} = {2}", _autoFilterText, item.GetType().Name, _autoFilterChecked);
        }
        else
        {
            Console.WriteLine("{0} : {1} = {2}", item.Text, item.GetType().Name, item.Checked);
        }
    }
}