コンボボックス型セルにデータ連結する方法

文書番号 : 40702     文書種別 : 使用方法     登録日 : 2017/07/26     最終更新日 : 2017/07/26
文書を印刷する
対象製品
MultiRow for Windows Forms 10.0J
詳細
コンボボックス型セルのDataSourceプロパティにデータセットを設定し、ValueMemberプロパティとDisplayMemberプロパティにそれぞれ値と表示用文字列を格納するバインドフィールドの名前を設定することで、コンボボックスコントロールと同じように連結したデータを表示できます。

GcComboBox型セルの場合は、コンボボックス型セルと同じようにDataSouceプロパティを設定したうえ、列のDataPropertyNameプロパティにバインドフィールドの名前を設定するとデータ連結できます。

下記のサンプルコード は、ComboBoxCell型セルとGcComboBoxCell型セルの項目にデータバインドをする方法を示しています。

  • コンボボックスのデータバインド

    [Visual Basic]
    Imports GrapeCity.Win.MultiRow
    Imports GrapeCity.Win.MultiRow.InputMan
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' コンボボックス型セル用データの作成
            Dim dl As New DataTable("LIST")
            dl.Columns.Add("Code", GetType(Int32))
            dl.Columns.Add("Name", GetType([String]))
            dl.Rows.Add(100, "A")
            dl.Rows.Add(200, "B")
            dl.Rows.Add(300, "C")
            dl.AcceptChanges()
    
            ' ComboBoxCell型セルの設定
            Dim comboCell As New ComboBoxCell()
            comboCell.Name = "comboCell"
            comboCell.DropDownWidth = 100
            comboCell.DataSource = dl
            comboCell.ValueMember = "Code"
            comboCell.DisplayMember = "Name"
            comboCell.DropDownStyle = MultiRowComboBoxStyle.DropDown
    
            ' GcComboBoxCell型セルの設定
            Dim gccomboCell As New GcComboBoxCell()
            gccomboCell.Name = "gccomboCell"
            gccomboCell.DataSource = dl
            gccomboCell.ListColumns.Clear()
            gccomboCell.ListColumns.Add("Code")
            gccomboCell.ListColumns.Add("Name")
            gccomboCell.ListColumns(0).DataPropertyName = "Code"
            gccomboCell.ListColumns(1).DataPropertyName = "Name"
            gccomboCell.ListColumns(0).Width = 40
            gccomboCell.ListColumns(1).Width = 60
            gccomboCell.DropDown.Width = 100
            gccomboCell.ValueSubItemIndex = 0
            gccomboCell.TextSubItemIndex = 1
    
            ' MultiRowの設定
            GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {comboCell, gccomboCell})
            GcMultiRow1.RowCount = 5
    
        End Sub
    End Class
    

    [C#]
    using GrapeCity.Win.MultiRow;
    using GrapeCity.Win.MultiRow.InputMan;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // コンボボックス型セル用データの作成
                DataTable dl = new DataTable("LIST");
                dl.Columns.Add("Code", typeof(Int32));
                dl.Columns.Add("Name", typeof(String));
                dl.Rows.Add(100, "A");
                dl.Rows.Add(200, "B");
                dl.Rows.Add(300, "C");
                dl.AcceptChanges();
    
                // ComboBoxCell型セルの設定
                ComboBoxCell comboCell = new ComboBoxCell();
                comboCell.Name = "comboCell";
                comboCell.DropDownWidth = 100;
                comboCell.DataSource = dl;
                comboCell.ValueMember = "Code";
                comboCell.DisplayMember = "Name";
                comboCell.DropDownStyle = MultiRowComboBoxStyle.DropDown;
    
                // GcComboBoxCell型セルの設定          
                GcComboBoxCell gccomboCell = new GcComboBoxCell();
                gccomboCell.Name = "gccomboCell";
                gccomboCell.DataField = "ColumnA";
                gccomboCell.DataSource = dl;
                gccomboCell.ListColumns.Clear();
                gccomboCell.ListColumns.Add("Code");
                gccomboCell.ListColumns.Add("Name");
                gccomboCell.ListColumns[0].DataPropertyName = "Code";
                gccomboCell.ListColumns[1].DataPropertyName = "Name";
                gccomboCell.ListColumns[0].Width = 40;
                gccomboCell.ListColumns[1].Width = 60;
                gccomboCell.DropDown.Width = 100;
                gccomboCell.ValueSubItemIndex = 0;
                gccomboCell.TextSubItemIndex = 1;
                
                // MultiRowの設定
                gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { comboCell, gccomboCell });
                gcMultiRow1.RowCount = 5;
            }
        }
    }
    

  • コンボボックスのドロップダウンリストの動的設定
    コンボボックスセルのドロップダウンリストの内容を動的に設定するには、編集コントロールのドロップダウンリストに設定する必要があります。
    下記のサンプルコードは、ComboBoxCell型セルとGcComboBoxCell型セルのドロップダウンリストを動的に設定する方法を示しています。

    [Visual Basic]
    Imports GrapeCity.Win.MultiRow
    Imports GrapeCity.Win.MultiRow.InputMan
    
    Public Class Form1
    
        Private dl1 As DataTable
        Private dl2 As DataTable
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' コンボボックス型セル用データの作成
            dl1 = New DataTable("LIST1")
            dl1.Columns.Add("Code", GetType(Int32))
            dl1.Columns.Add("Name", GetType([String]))
            dl1.Rows.Add(100, "A")
            dl1.Rows.Add(200, "B")
            dl1.Rows.Add(300, "C")
            dl1.AcceptChanges()
            dl2 = New DataTable("LIST2")
            dl2.Columns.Add("Code", GetType(Int32))
            dl2.Columns.Add("Name", GetType([String]))
            dl2.Rows.Add(100, "D")
            dl2.Rows.Add(200, "E")
            dl2.Rows.Add(300, "F")
            dl2.AcceptChanges()
    
            ' コンボボックス型セルの設定
            Dim textCell As New TextBoxCell()
            textCell.Name = "textCell"
            textCell.Size = New Size(60, textCell.Height)
            Dim comboCellA As New ComboBoxCell()
            comboCellA.Name = "comboCellA"
            comboCellA.DropDownWidth = 100
            comboCellA.ShowDropDownButton = CellButtonVisibility.ShowForCurrentCell
            comboCellA.MaxLength = 3
            comboCellA.DropDownStyle = MultiRowComboBoxStyle.DropDown
            Dim comboCellB As New GcComboBoxCell()
            comboCellB.Name = "comboCellB"
            comboCellB.DropDown.Width = 100
            comboCellB.SideButtons(0).Visible = CellButtonVisibility.ShowForCurrentCell
            comboCellB.MaxLength = 3
            comboCellB.DropDownStyle = MultiRowComboBoxStyle.DropDown
            comboCellB.ListColumns.Add("Code")
            comboCellB.ListColumns.Add("Name")
            comboCellB.ListColumns(0).Width = 50
            comboCellB.ListColumns(1).Width = 50
    
            ' MultiRowの設定
            GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {textCell, comboCellA, comboCellB})
            GcMultiRow1.RowCount = 5
            GcMultiRow1.SetValue(0, 0, "1")
            GcMultiRow1.SetValue(1, 0, "2")
            GcMultiRow1.SetValue(2, 0, "3")
    
        End Sub
    
        Private Sub GcMultiRow1_EditingControlShowing(sender As Object, e As EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing
            ' MultiRowの取得
            Dim grid As GcMultiRow = TryCast(sender, GcMultiRow)
    
            ' カレント行番号の取得
            Dim row As Integer = grid.CurrentCellPosition.RowIndex
    
            ' コンボボックス型セルのリストの動的な設定
            If TypeOf e.Control Is ComboBoxEditingControl Then
                ' ComboBoxCell型セルの場合
                Dim comboA As ComboBoxEditingControl = TryCast(e.Control, ComboBoxEditingControl)
                comboA.Items.Clear()
                Select Case String.Concat(grid.GetValue(row, "textCell"))
                    Case "1"
                        Dim list1 As DataRow() = dl1.[Select]("", "Code ASC")
                        For i As Integer = 0 To list1.Length - 1
                            Dim item As String = String.Format("{0} {1}", list1(i)("Code"), list1(i)("Name"))
                            comboA.Items.Add(item)
                        Next
                        Exit Select
                    Case "2"
                        Dim list2 As DataRow() = dl2.[Select]("", "Code ASC")
                        For i As Integer = 0 To list2.Length - 1
                            Dim item As String = String.Format("{0} {1}", list2(i)("Code"), list2(i)("Name"))
                            comboA.Items.Add(item)
                        Next
                        Exit Select
                    Case Else
                        Exit Select
                End Select
            ElseIf TypeOf e.Control Is GcComboBoxEditingControl Then
                ' GcComboBoxCell型セルの場合
                Dim comboB As GcComboBoxEditingControl = TryCast(e.Control, GcComboBoxEditingControl)
                comboB.Items.Clear()
                Select Case String.Concat(grid.GetValue(row, "textCell"))
                    Case "1"
                        Dim list1 As DataRow() = dl1.[Select]("", "Code ASC")
                        For i As Integer = 0 To list1.Length - 1
                            Dim item11 As New GrapeCity.Win.Editors.SubItem(list1(i)("Code"))
                            Dim item12 As New GrapeCity.Win.Editors.SubItem(list1(i)("Name"))
                            comboB.Items.Add(New GrapeCity.Win.Editors.ListItem(New GrapeCity.Win.Editors.SubItem() {item11, item12}))
                        Next
                        Exit Select
                    Case "2"
                        Dim list2 As DataRow() = dl2.[Select]("", "Code ASC")
                        For i As Integer = 0 To list2.Length - 1
                            Dim item21 As New GrapeCity.Win.Editors.SubItem(list2(i)("Code"))
                            Dim item22 As New GrapeCity.Win.Editors.SubItem(list2(i)("Name"))
                            comboB.Items.Add(New GrapeCity.Win.Editors.ListItem(New GrapeCity.Win.Editors.SubItem() {item21, item22}))
                        Next
                        Exit Select
                    Case Else
                        Exit Select
                End Select
            End If
        End Sub
    End Class
    

    [C#]
    using GrapeCity.Win.MultiRow;
    using GrapeCity.Win.MultiRow.InputMan;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private DataTable dl1;
            private DataTable dl2;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // コンボボックス型セル用データの作成
                dl1 = new DataTable("LIST1");
                dl1.Columns.Add("Code", typeof(Int32));
                dl1.Columns.Add("Name", typeof(String));
                dl1.Rows.Add(100, "A");
                dl1.Rows.Add(200, "B");
                dl1.Rows.Add(300, "C");
                dl1.AcceptChanges();
                dl2 = new DataTable("LIST2");
                dl2.Columns.Add("Code", typeof(Int32));
                dl2.Columns.Add("Name", typeof(String));
                dl2.Rows.Add(100, "D");
                dl2.Rows.Add(200, "E");
                dl2.Rows.Add(300, "F");
                dl2.AcceptChanges();
    
                // コンボボックス型セルの設定
                TextBoxCell textCell = new TextBoxCell();
                textCell.Name = "textCell";
                textCell.Size = new Size(60, textCell.Height);
                ComboBoxCell comboCellA = new ComboBoxCell();
                comboCellA.Name = "comboCellA";
                comboCellA.DropDownWidth = 100;
                comboCellA.ShowDropDownButton = CellButtonVisibility.ShowForCurrentCell;
                comboCellA.MaxLength = 3;
                comboCellA.DropDownStyle = MultiRowComboBoxStyle.DropDown;
                GcComboBoxCell comboCellB = new GcComboBoxCell();
                comboCellB.Name = "comboCellB";
                comboCellB.DropDown.Width = 100;
                comboCellB.SideButtons[0].Visible = CellButtonVisibility.ShowForCurrentCell;
                comboCellB.MaxLength = 3;
                comboCellB.DropDownStyle = MultiRowComboBoxStyle.DropDown;
                comboCellB.ListColumns.Add("Code");
                comboCellB.ListColumns.Add("Name");
                comboCellB.ListColumns[0].Width = 50;
                comboCellB.ListColumns[1].Width = 50;
    
                // MultiRowの設定
                gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { textCell, comboCellA, comboCellB });
                gcMultiRow1.RowCount = 5;
                gcMultiRow1.SetValue(0, 0, "1");
                gcMultiRow1.SetValue(1, 0, "2");
                gcMultiRow1.SetValue(2, 0, "3");
    
            }
    
            private void gcMultiRow1_EditingControlShowing(object sender, EditingControlShowingEventArgs e)
            {
                // MultiRowの取得
                GcMultiRow grid = sender as GcMultiRow;
    
                // カレント行番号の取得
                int row = grid.CurrentCellPosition.RowIndex;
    
                // コンボボックス型セルのリストの動的な設定
                if (e.Control is ComboBoxEditingControl)
                {
                    // ComboBoxCell型セルの場合
                    ComboBoxEditingControl comboA = e.Control as ComboBoxEditingControl;
                    comboA.Items.Clear();
                    switch (string.Concat(grid.GetValue(row, "textCell")))
                    {
                        case "1":
                            DataRow[] list1 = dl1.Select("", "Code ASC");
                            for (int i = 0; i < list1.Length; i++)
                            {
                                string item = string.Format("{0} {1}", list1[i]["Code"], list1[i]["Name"]);
                                comboA.Items.Add(item);
                            }
                            break;
                        case "2":
                            DataRow[] list2 = dl2.Select("", "Code ASC");
                            for (int i = 0; i < list2.Length; i++)
                            {
                                string item = string.Format("{0} {1}", list2[i]["Code"], list2[i]["Name"]);
                                comboA.Items.Add(item);
                            }
                            break;
                        default:
                            break;
                    }
                }
                else if (e.Control is GcComboBoxEditingControl)
                {
                    // GcComboBoxCell型セルの場合
                    GcComboBoxEditingControl comboB = e.Control as GcComboBoxEditingControl;
                    comboB.Items.Clear();
                    switch (string.Concat(grid.GetValue(row, "textCell")))
                    {
                        case "1":
                            DataRow[] list1 = dl1.Select("", "Code ASC");
                            for (int i = 0; i < list1.Length; i++)
                            {
                                GrapeCity.Win.Editors.SubItem item11 = new GrapeCity.Win.Editors.SubItem(list1[i]["Code"]);
                                GrapeCity.Win.Editors.SubItem item12 = new GrapeCity.Win.Editors.SubItem(list1[i]["Name"]);
                                comboB.Items.Add(new GrapeCity.Win.Editors.ListItem(new GrapeCity.Win.Editors.SubItem[] { item11, item12 }));
                            }
                            break;
                        case "2":
                            DataRow[] list2 = dl2.Select("", "Code ASC");
                            for (int i = 0; i < list2.Length; i++)
                            {
                                GrapeCity.Win.Editors.SubItem item21 = new GrapeCity.Win.Editors.SubItem(list2[i]["Code"]);
                                GrapeCity.Win.Editors.SubItem item22 = new GrapeCity.Win.Editors.SubItem(list2[i]["Name"]);
                                comboB.Items.Add(new GrapeCity.Win.Editors.ListItem(new GrapeCity.Win.Editors.SubItem[] { item21, item22 }));
                            }
                            break;
                        default:
                            break;
                    }
                }
    
            }
        }
    }