PowerTools MultiRow for Windows Forms 8.0J
DropDownList プロパティ
使用例 

ドロップダウンリストに関する情報のセットを取得または設定します。
構文
Public Property DropDownList As HeaderDropDownList
public HeaderDropDownList DropDownList {get; set;}

プロパティ値

ドロップダウンリストに関する情報のセットを表すHeaderDropDownList値。既定値はnull 参照 (Visual Basicでは Nothing)です。
解説

このプロパティを通じて、並べ替え操作やフィルタ操作を実行できます。

HeaderDropDownListを作成するときは、HeaderDropDownListHeaderDropDownList.ItemsコレクションにDropDownItemを追加します。このコレクションは、このDropDownListによって実行できるアクションを決定します。作成したHeaderDropDownListをこのプロパティに設定すると、ColumnHeaderCellにドロップダウンボタンが表示され、それをクリックするとドロップダウンリストが表示されます。このリストの項目をクリックして、アクションを実行できます。また、HeaderDropDownListのプロパティを設定することで、ドロップダウンリストのスタイル(HeaderDropDownList.DropDownWidthHeaderDropDownList.Alignmentなど)を制御できます。

使用例
次のサンプルコードは、並べ替えまたはフィルタを実行するためのドロップダウンリストを列ヘッダセルに作成する方法を示します。ここでは、単純な既定のドロップダウンリストと複雑なカスタムフィルタ項目を実装しています。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace GrapeCity.Win.MultiRow.SampleCode
{
    public class HeaderDropDownListDemo : Form
    {
        private GcMultiRow gcMultiRow1 = new GcMultiRow();
        private FlowLayoutPanel panel = new FlowLayoutPanel();

        public HeaderDropDownListDemo()
        {
            this.Text = "HeaderDropDownList Demo";
            this.Size = new Size(630, 350);

            // Initial flow layout panel and add to form.
            this.panel.Dock = DockStyle.Left;
            this.panel.Size = new Size(250, 200);
            this.panel.FlowDirection = FlowDirection.TopDown;
            this.panel.WrapContents = false;
            this.panel.Padding = new Padding(5);
            this.Controls.Add(panel);

            // Add MultiRow to form
            this.gcMultiRow1.Dock = DockStyle.Left;
            this.gcMultiRow1.Width = 380;
            this.Controls.Add(this.gcMultiRow1);

            this.Load += new EventHandler(Form1_Load);

            InitButton();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a template with 4 text box cell.
            gcMultiRow1.Template = Template.CreateGridTemplate(4);

            gcMultiRow1.RowCount = 100;

            // Fill values.
            for (int rowIndex = 0; rowIndex < gcMultiRow1.RowCount - 1; rowIndex++)
            {
                for (int cellIndex = 0; cellIndex < 4; cellIndex++)
                {
                    gcMultiRow1[rowIndex, cellIndex].Value = rowIndex * 10 + cellIndex;
                }
            }
        }

        #region Button Event Handlers

        void setFirstColumnDropDownListButton_Click(object sender, EventArgs e)
        {
            // Get first column header cell.
            ColumnHeaderCell columnHeaderCell = this.gcMultiRow1.ColumnHeaders[0][0] as ColumnHeaderCell;

            // Create a drop down list with some default down down items.
            columnHeaderCell.DropDownList = new HeaderDropDownList(0, true, true);
        }

        void setSecondColumnDropDownListButton_Click(object sender, EventArgs e)
        {
            // Create a header drop down list without default down down items.
            HeaderDropDownList headerDropDownList = new HeaderDropDownList();

            // If do not indicate which cell to be filtered, the cell which has same index with column header cell in row section will be filtered.
            headerDropDownList.CellName = "textBoxCell2";

            DropDownItemCollection dropDownItemCollection = headerDropDownList.Items;
            // Initialize drop down items manually.
            // Add sort item.
            dropDownItemCollection.Add(new DropDownSortItem(SortOrder.Ascending));
            dropDownItemCollection.Add(new DropDownSortItem(SortOrder.Descending));
            
            // Add a separator line.
            dropDownItemCollection.Add(new DropDownSeparatorItem());

            // Add show all, black and none black items.
            dropDownItemCollection.Add(new DropDownShowAllFilterItem());
            dropDownItemCollection.Add(new DropDownBlanksFilterItem());
            dropDownItemCollection.Add(new DropDownNonBlanksFilterItem());

            // Add auto filter items.
            dropDownItemCollection.Add(new DropDownAutoFilterItem());

            // Get second column header cell.
            ColumnHeaderCell columnHeaderCell = this.gcMultiRow1.ColumnHeaders[0][1] as ColumnHeaderCell;
            columnHeaderCell.DropDownList = headerDropDownList;
        }

        void setThirdColumnDropDownListButton_Click(object sender, EventArgs e)
        {
            // Create a header drop down list without default down down items.
            HeaderDropDownList headerDropDownList = new HeaderDropDownList();

            DropDownItemCollection dropDownItemCollection = headerDropDownList.Items;

            // Initialize drop down items manually.
            // Add show all item.
            dropDownItemCollection.Add(new DropDownShowAllFilterItem());

            DropDownAutoFilterItem autoFilterItem = new DropDownAutoFilterItem();

            autoFilterItem.DropDownItemNeeded += new EventHandler<DropDownItemNeededEventArgs>(autoFilterItem_DropDownItemNeeded);

            // custom range item.
            dropDownItemCollection.Add(autoFilterItem);

            // Get second column header cell.
            ColumnHeaderCell columnHeaderCell = this.gcMultiRow1.ColumnHeaders[0][2] as ColumnHeaderCell;
            columnHeaderCell.DropDownList = headerDropDownList;
        }

        void autoFilterItem_DropDownItemNeeded(object sender, DropDownItemNeededEventArgs e)
        {
            int filterValue = int.Parse(e.FilterValue.ToString());

            e.DropDownCustomFilterItem = new AutoRangeFilterItem(filterValue / 100);

            e.Handled = true;
        }

        public class AutoRangeFilterItem : DropDownCustomFilterItem
        {
            public AutoRangeFilterItem(int filterValue)
                : base(filterValue)
            {

            }

            public override string Text
            {
                get
                {
                    return ((int)FilterValue * 100).ToString() + " to " + (((int)FilterValue + 1) * 100).ToString();
                }
                set { }
            }

            protected override bool Check(object value)
            {
                // check the value whether in specific range.
                int checkedValue = int.Parse(value.ToString());

                return (checkedValue >= ((int)FilterValue * 100) && checkedValue <= (((int)FilterValue + 1) * 100));
            }
        }

        void setFourthColumnDropDownListButton_Click(object sender, EventArgs e)
        {
            // Create a header drop down list without default down down items.
            HeaderDropDownList headerDropDownList = new HeaderDropDownList();

            DropDownItemCollection dropDownItemCollection = headerDropDownList.Items;

            // Initialize drop down items manually.
            // Add show all item.
            dropDownItemCollection.Add(new DropDownShowAllFilterItem());

            // custom range item.
            dropDownItemCollection.Add(new PopupFilterItem());

            // Get second column header cell.
            ColumnHeaderCell columnHeaderCell = this.gcMultiRow1.ColumnHeaders[0][3] as ColumnHeaderCell;
            columnHeaderCell.DropDownList = headerDropDownList;
        }

        public class PopupFilterItem : DropDownCustomFilterItem
        {
            public override string Text
            {
                get
                {
                    if (this.Checked)
                    {
                        return min.ToString() + " to " + max.ToString();
                    }
                    return "(Custom)";
                }
                set { }
            }

            int max = -1;
            int min = -1;

            protected override bool Check(object value)
            {
                // check the value whether in specific range.
                int checkedValue = int.Parse(value.ToString());

                return (checkedValue >= min && checkedValue <= max);
            }

            protected override void OnClick(EventArgs e)
            {
                // Initialize pop up form.
                Form form = new Form();

                FlowLayoutPanel panel = new FlowLayoutPanel();
                panel.Height = form.Height;

                Label label1 = new Label();
                label1.Text = "Min Value:";

                Label label2 = new Label();
                label2.Text = "MaxValue:";

                NumericUpDown numericUpDown1 = new NumericUpDown();

                NumericUpDown numericUpDown2 = new NumericUpDown();
                numericUpDown2.Maximum = 1000;
                numericUpDown2.Value = 50;

                Button okButton = new Button();
                okButton.Text = "OK";
                okButton.DialogResult = DialogResult.OK;
                form.AcceptButton = okButton;

                form.Controls.Add(panel);
                panel.Controls.Add(label1);
                panel.Controls.Add(numericUpDown1);
                panel.Controls.Add(label2);
                panel.Controls.Add(numericUpDown2);
                panel.Controls.Add(okButton);

                DialogResult result = form.ShowDialog();

                // If input a range and click OK button. Update range.
                if (result == DialogResult.OK)
                {
                    min = (int)(numericUpDown1.Value);
                    max = (int)(numericUpDown2.Value);
                }
                else
                {
                    min = int.MinValue;
                    max = int.MaxValue;
                }

                base.OnClick(e);
            }
        }

        #endregion

        #region Initialize Buttons

        private void InitButton()
        {
            AddButton(setFirstColumnDropDownListButton, "Set 1st column drop down list", new EventHandler(setFirstColumnDropDownListButton_Click));
            AddButton(setSecondColumnDropDownListButton, "Set 2nd column drop down list", new EventHandler(setSecondColumnDropDownListButton_Click));
            AddButton(setThirdColumnDropDownListButton, "Set 3rd column custom drop down list", new EventHandler(setThirdColumnDropDownListButton_Click));
            AddButton(setFourthColumnDropDownListButton, "Set 4th column custom drop down list", new EventHandler(setFourthColumnDropDownListButton_Click));
        }

        private void AddButton(Button button, string text, EventHandler eventHandler)
        {
            this.panel.Controls.Add(button);
            button.Text = text;
            button.AutoSize = true;
            button.Click += eventHandler;
        }

        Button setFirstColumnDropDownListButton = new Button();
        Button setSecondColumnDropDownListButton = new Button();
        Button setThirdColumnDropDownListButton = new Button();
        Button setFourthColumnDropDownListButton = new Button();

        #endregion

        [STAThreadAttribute()]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new HeaderDropDownListDemo());
        }
    }
}
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports GrapeCity.Win.MultiRow

Public Class HeaderDropDownListDemo
    Inherits Form
    Private gcMultiRow1 As New GcMultiRow()
    Private panel As New FlowLayoutPanel()

    Public Sub New()
        Me.Text = "HeaderDropDownList Demo"
        Me.Size = New Size(630, 350)

        ' Initial flow layout panel and add to form.
        Me.panel.Dock = DockStyle.Left
        Me.panel.Size = New Size(250, 200)
        Me.panel.FlowDirection = FlowDirection.TopDown
        Me.panel.WrapContents = False
        Me.panel.Padding = New Padding(5)
        Me.Controls.Add(panel)

        ' Add MultiRow to form
        Me.gcMultiRow1.Dock = DockStyle.Left
        Me.gcMultiRow1.Width = 380
        Me.Controls.Add(Me.gcMultiRow1)

        InitButton()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        ' Create a template with 4 text box cell.
        gcMultiRow1.Template = Template.CreateGridTemplate(4)

        gcMultiRow1.RowCount = 100

        ' Fill values.

        For rowIndex As Integer = 0 To gcMultiRow1.RowCount - 2
            For cellIndex As Integer = 0 To 3
                gcMultiRow1(rowIndex, cellIndex).Value = rowIndex * 10 + cellIndex
            Next
        Next
    End Sub

#Region "Button Event Handlers"

    Private Sub setFirstColumnDropDownListButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setFirstColumnDropDownListButton.Click
        ' Get first column header cell.
        Dim columnHeaderCell As ColumnHeaderCell = TryCast(Me.gcMultiRow1.ColumnHeaders(0)(0), ColumnHeaderCell)

        ' Create a drop down list with some default down down items.
        columnHeaderCell.DropDownList = New HeaderDropDownList(0, True, True)
    End Sub

    Private Sub setSecondColumnDropDownListButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setSecondColumnDropDownListButton.Click
        ' Create a header drop down list without default down down items.
        Dim headerDropDownList As New HeaderDropDownList()

        ' If do not indicate which cell to be filtered, the cell which has same index with column header cell in row section will be filtered.
        headerDropDownList.CellName = "textBoxCell2"

        Dim dropDownItemCollection As DropDownItemCollection = headerDropDownList.Items
        ' Initialize drop down items manually.
        ' Add sort item.
        dropDownItemCollection.Add(New DropDownSortItem(SortOrder.Ascending))
        dropDownItemCollection.Add(New DropDownSortItem(SortOrder.Descending))

        ' Add a separator line.
        dropDownItemCollection.Add(New DropDownSeparatorItem())

        ' Add show all, black and none black items.
        dropDownItemCollection.Add(New DropDownShowAllFilterItem())
        dropDownItemCollection.Add(New DropDownBlanksFilterItem())
        dropDownItemCollection.Add(New DropDownNonBlanksFilterItem())

        ' Add auto filter items.
        dropDownItemCollection.Add(New DropDownAutoFilterItem())

        ' Get second column header cell.
        Dim columnHeaderCell As ColumnHeaderCell = TryCast(Me.gcMultiRow1.ColumnHeaders(0)(1), ColumnHeaderCell)
        columnHeaderCell.DropDownList = headerDropDownList
    End Sub

    Private Sub setThirdColumnDropDownListButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setThirdColumnDropDownListButton.Click
        ' Create a header drop down list without default down down items.
        Dim headerDropDownList As New HeaderDropDownList()

        Dim dropDownItemCollection As DropDownItemCollection = headerDropDownList.Items

        ' Initialize drop down items manually.
        ' Add show all item.
        dropDownItemCollection.Add(New DropDownShowAllFilterItem())

        Dim autoFilterItem As New DropDownAutoFilterItem()

        AddHandler autoFilterItem.DropDownItemNeeded, AddressOf autoFilterItem_DropDownItemNeeded

        ' custom range item.
        dropDownItemCollection.Add(autoFilterItem)

        ' Get second column header cell.
        Dim columnHeaderCell As ColumnHeaderCell = TryCast(Me.gcMultiRow1.ColumnHeaders(0)(2), ColumnHeaderCell)
        columnHeaderCell.DropDownList = headerDropDownList
    End Sub

    Private Sub autoFilterItem_DropDownItemNeeded(ByVal sender As Object, ByVal e As DropDownItemNeededEventArgs)
        Dim filterValue As Integer = Integer.Parse(e.FilterValue.ToString())

        e.DropDownCustomFilterItem = New AutoRangeFilterItem(filterValue / 100)

        e.Handled = True
    End Sub

    Public Class AutoRangeFilterItem
        Inherits DropDownCustomFilterItem
        Public Sub New(ByVal filterValue As Integer)

            MyBase.New(filterValue)
        End Sub

        Public Overloads Overrides Property Text() As String
            Get
                Return (DirectCast(FilterValue, Integer) * 100).ToString() + " to " + ((DirectCast(FilterValue, Integer) + 1) * 100).ToString()
            End Get
            Set(ByVal value As String)
            End Set
        End Property

        Protected Overloads Overrides Function Check(ByVal value As Object) As Boolean
            ' check the value whether in specific range.
            Dim checkedValue As Integer = Integer.Parse(value.ToString())

            Return (checkedValue >= (DirectCast(FilterValue, Integer) * 100) AndAlso checkedValue <= ((DirectCast(FilterValue, Integer) + 1) * 100))
        End Function
    End Class

    Private Sub setFourthColumnDropDownListButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setFourthColumnDropDownListButton.Click
        ' Create a header drop down list without default down down items.
        Dim headerDropDownList As New HeaderDropDownList()

        Dim dropDownItemCollection As DropDownItemCollection = headerDropDownList.Items

        ' Initialize drop down items manually.
        ' Add show all item.
        dropDownItemCollection.Add(New DropDownShowAllFilterItem())

        ' custom range item.
        dropDownItemCollection.Add(New PopupFilterItem())

        ' Get second column header cell.
        Dim columnHeaderCell As ColumnHeaderCell = TryCast(Me.gcMultiRow1.ColumnHeaders(0)(3), ColumnHeaderCell)
        columnHeaderCell.DropDownList = headerDropDownList
    End Sub

    Public Class PopupFilterItem
        Inherits DropDownCustomFilterItem
        Public Overloads Overrides Property Text() As String
            Get
                If Me.Checked Then
                    Return min.ToString() + " to " + max.ToString()
                End If
                Return "(Custom)"
            End Get
            Set(ByVal value As String)
            End Set
        End Property

        Private max As Integer = -1
        Private min As Integer = -1

        Protected Overloads Overrides Function Check(ByVal value As Object) As Boolean
            ' check the value whether in specific range.
            Dim checkedValue As Integer = Integer.Parse(value.ToString())

            Return (checkedValue >= min AndAlso checkedValue <= max)
        End Function

        Protected Overloads Overrides Sub OnClick(ByVal e As EventArgs)
            ' Initialize pop up form.
            Dim form As New Form()

            Dim panel As New FlowLayoutPanel()
            panel.Height = form.Height

            Dim label1 As New Label()
            label1.Text = "Min Value:"

            Dim label2 As New Label()
            label2.Text = "MaxValue:"

            Dim numericUpDown1 As New NumericUpDown()

            Dim numericUpDown2 As New NumericUpDown()
            numericUpDown2.Maximum = 1000
            numericUpDown2.Value = 50

            Dim okButton As New Button()
            okButton.Text = "OK"
            okButton.DialogResult = DialogResult.OK
            form.AcceptButton = okButton

            form.Controls.Add(panel)
            panel.Controls.Add(label1)
            panel.Controls.Add(numericUpDown1)
            panel.Controls.Add(label2)
            panel.Controls.Add(numericUpDown2)
            panel.Controls.Add(okButton)

            Dim result As DialogResult = form.ShowDialog()

            ' If input a range and click OK button. Update range.
            If result = DialogResult.OK Then
                min = CInt(numericUpDown1.Value)
                max = CInt(numericUpDown2.Value)
            Else
                min = Integer.MinValue
                max = Integer.MaxValue
            End If

            MyBase.OnClick(e)
        End Sub
    End Class

#End Region

#Region "Initialize Buttons"

    Private Sub InitButton()
        AddButton(setFirstColumnDropDownListButton, "Set 1st column drop down list")
        AddButton(setSecondColumnDropDownListButton, "Set 2nd column drop down list")
        AddButton(setThirdColumnDropDownListButton, "Set 3rd column custom drop down list")
        AddButton(setFourthColumnDropDownListButton, "Set 4th column custom drop down list")
    End Sub

    Private Sub AddButton(ByVal button As Button, ByVal text As String)
        Me.panel.Controls.Add(button)
        button.Text = text
        button.AutoSize = True
    End Sub

    Friend WithEvents setFirstColumnDropDownListButton As New Button()
    Friend WithEvents setSecondColumnDropDownListButton As New Button()
    Friend WithEvents setThirdColumnDropDownListButton As New Button()
    Friend WithEvents setFourthColumnDropDownListButton As New Button()

#End Region

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New HeaderDropDownListDemo())
    End Sub
End Class
参照

ColumnHeaderCell クラス
ColumnHeaderCell メンバ
HeaderDropDownList クラス
DropDownItem クラス

 

 


© 2008-2015 GrapeCity inc. All rights reserved.