[GcSpreadGrid] コンボボックス型セルの基本的な使用例
対象製品
SPREAD for WPF 3.0J
詳細
コンボボックス型セルの基本的な使用例を記載します。
1.表示値/実値のように2種類のデータを持たせる
2.任意の項目を表示(選択)させる(初期値の設定)
3.項目を追加する
4.セル毎に項目を動的に変更する
※コンボボックスに連結しているサンプルデータ
1.表示値/実値のように2種類のデータを持たせる
ComboBoxCellType クラスのContentPath プロパティによりリストに表示される項目、SelectedValuePath プロパティによりリストの表示項目に対する実値を設定することができます。また、ValueType プロパティにより参照するセルの値の種類を指定することができます。
◎サンプルコード(VB)
◎サンプルコード(C#)
2.任意の項目を表示(選択)させる(初期値の設定)
セルのValue プロパティからコードで任意の項目を表示させることができます。なお、ComboBoxCellType クラスのValueType プロパティにより、セルのValue プロパティに設定する値の種類を変更します。
◎サンプルコード(VB)
◎サンプルコード(C#)
3.項目を追加する
コンボックスクラスとバインドしているコレクションに項目を追加することでコンボックス型セルにも項目を追加します。なお、このサンプルでは項目の追加や変更が必要になるため、INotifyPropertyChangedインターフェイスとINotifyCollectionChangedインターフェイスがあらかじめ実装されているObservableCollectionクラスとバインドしています。ObservableCollectionクラスの詳細についてはMSDNライブラリをご参照ください。
◎サンプルコード(VB)
◎サンプルコード(C#)
4.セル毎に項目を動的に変更する
セル毎に項目を動的に変更する場合、セル毎にコンボボックス型セルのインスタンスを作成する必要があります。
◎サンプルコード(VB)
◎サンプルコード(C#)
【コンボボックスに連結しているクラス】
◎サンプルコード(VB)
◎サンプルコード(C#)
1.表示値/実値のように2種類のデータを持たせる
2.任意の項目を表示(選択)させる(初期値の設定)
3.項目を追加する
4.セル毎に項目を動的に変更する
※コンボボックスに連結しているサンプルデータ
1.表示値/実値のように2種類のデータを持たせる
ComboBoxCellType クラスのContentPath プロパティによりリストに表示される項目、SelectedValuePath プロパティによりリストの表示項目に対する実値を設定することができます。また、ValueType プロパティにより参照するセルの値の種類を指定することができます。
◎サンプルコード(VB)
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded ' ◆1. 表示値/実値のように2種類のデータを持たせる Dim c1 As New GrapeCity.Windows.SpreadGrid.ComboBoxCellType() c1.ItemsSource = ItemData.GetData() ' リストに表示されるアイテムを定義します c1.ContentPath = "Text" ' 表示される各アイテムに対応したデータを定義します c1.SelectedValuePath = "Value" gcSpreadGrid1.Cells(1, 1).CellType = c1 End Sub Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Dim check As [String] = [String].Empty check += "表示値:" & gcSpreadGrid1.Cells(1, 1).Text.ToString() & vbCr & vbLf check += "実値:" & gcSpreadGrid1.Cells(1, 1).Value.ToString() MessageBox.Show(check, "確認") End Sub
◎サンプルコード(C#)
private void Window_Loaded(object sender, RoutedEventArgs e){ // ◆1. 表示値/実値のように2種類のデータを持たせる GrapeCity.Windows.SpreadGrid.ComboBoxCellType c1 = new GrapeCity.Windows.SpreadGrid.ComboBoxCellType(); c1.ItemsSource = ItemData.GetData(); // リストに表示されるアイテムを定義します c1.ContentPath = "Text"; // 表示される各アイテムに対応したデータを定義します c1.SelectedValuePath = "Value"; gcSpreadGrid1.Cells[1, 1].CellType = c1; } private void button1_Click(object sender, RoutedEventArgs e) { String check = String.Empty; check += "表示値:" + gcSpreadGrid1.Cells[1, 1].Text.ToString() + "¥r¥n"; check += "実値:" + gcSpreadGrid1.Cells[1, 1].Value.ToString(); MessageBox.Show(check, "確認"); }
2.任意の項目を表示(選択)させる(初期値の設定)
セルのValue プロパティからコードで任意の項目を表示させることができます。なお、ComboBoxCellType クラスのValueType プロパティにより、セルのValue プロパティに設定する値の種類を変更します。
◎サンプルコード(VB)
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded ' ◆2. 任意の項目を表示(選択)させる Dim c2 As New GrapeCity.Windows.SpreadGrid.ComboBoxCellType() c2.ItemsSource = ItemData.GetData() c2.ContentPath = "Text" c2.SelectedValuePath = "Value" ' セルから取得/設定する値は文字列 c2.ValueType = ComboBoxValueType.Text gcSpreadGrid1.Cells(2, 2).CellType = c2 gcSpreadGrid1.Cells(2, 2).Value = "東京" Dim c3 As New GrapeCity.Windows.SpreadGrid.ComboBoxCellType() c3.ItemsSource = ItemData.GetData() c3.ContentPath = "Text" c3.SelectedValuePath = "Value" ' セルから取得/設定する値はIndex c3.ValueType = ComboBoxValueType.SelectedIndex gcSpreadGrid1.Cells(2, 3).CellType = c3 gcSpreadGrid1.Cells(2, 3).Value = 1 Dim c4 As New GrapeCity.Windows.SpreadGrid.ComboBoxCellType() c4.ItemsSource = ItemData.GetData() c4.ContentPath = "Text" c4.SelectedValuePath = "Value" ' セルから取得/設定する値はValue c4.ValueType = ComboBoxValueType.SelectedValue gcSpreadGrid1.Cells(2, 4).CellType = c4 gcSpreadGrid1.Cells(2, 4).Value = "003" End Sub
◎サンプルコード(C#)
private void Window_Loaded(object sender, RoutedEventArgs e){ // ◆2. 任意の項目を表示(選択)させる GrapeCity.Windows.SpreadGrid.ComboBoxCellType c2 = new GrapeCity.Windows.SpreadGrid.ComboBoxCellType(); c2.ItemsSource = ItemData.GetData(); c2.ContentPath = "Text"; c2.SelectedValuePath = "Value"; // セルから取得/設定する値は文字列 c2.ValueType = ComboBoxValueType.Text; gcSpreadGrid1.Cells[2, 2].CellType = c2; gcSpreadGrid1.Cells[2, 2].Value = "東京"; GrapeCity.Windows.SpreadGrid.ComboBoxCellType c3 = new GrapeCity.Windows.SpreadGrid.ComboBoxCellType(); c3.ItemsSource = ItemData.GetData(); c3.ContentPath = "Text"; c3.SelectedValuePath = "Value"; // セルから取得/設定する値はIndex c3.ValueType = ComboBoxValueType.SelectedIndex; gcSpreadGrid1.Cells[2, 3].CellType = c3; gcSpreadGrid1.Cells[2, 3].Value = 1; GrapeCity.Windows.SpreadGrid.ComboBoxCellType c4 = new GrapeCity.Windows.SpreadGrid.ComboBoxCellType(); c4.ItemsSource = ItemData.GetData(); c4.ContentPath = "Text"; c4.SelectedValuePath = "Value"; // セルから取得/設定する値はValue c4.ValueType = ComboBoxValueType.SelectedValue; gcSpreadGrid1.Cells[2, 4].CellType = c4; gcSpreadGrid1.Cells[2, 4].Value = "003"; }
3.項目を追加する
コンボックスクラスとバインドしているコレクションに項目を追加することでコンボックス型セルにも項目を追加します。なお、このサンプルでは項目の追加や変更が必要になるため、INotifyPropertyChangedインターフェイスとINotifyCollectionChangedインターフェイスがあらかじめ実装されているObservableCollection
◎サンプルコード(VB)
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded ' ◆3. 項目を追加する Dim c5 As New GrapeCity.Windows.SpreadGrid.ComboBoxCellType() c5.ItemsSource = New ItemDataCollection() c5.ContentPath = "Text" c5.SelectedValuePath = "Value" gcSpreadGrid1.Cells(3, 3).CellType = c5 End Sub Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Dim c As GrapeCity.Windows.SpreadGrid.ComboBoxCellType = DirectCast(gcSpreadGrid1.Cells(3, 3).InheritedCellType, GrapeCity.Windows.SpreadGrid.ComboBoxCellType) ' コンボボックスとバインドしているコレクションにデータを追加します Dim lst As ItemDataCollection = DirectCast(c.ItemsSource, ItemDataCollection) lst.Add(New ItemData() With { _ .Text = "福岡", _ .Value = "004" _ }) End Sub
◎サンプルコード(C#)
private void Window_Loaded(object sender, RoutedEventArgs e){ // ◆3. 項目を追加する GrapeCity.Windows.SpreadGrid.ComboBoxCellType c5 = new GrapeCity.Windows.SpreadGrid.ComboBoxCellType(); c5.ItemsSource = new ItemDataCollection(); c5.ContentPath = "Text"; c5.SelectedValuePath = "Value"; gcSpreadGrid1.Cells[3, 3].CellType = c5; } private void button2_Click(object sender, RoutedEventArgs e) { GrapeCity.Windows.SpreadGrid.ComboBoxCellType c = (GrapeCity.Windows.SpreadGrid.ComboBoxCellType)gcSpreadGrid1.Cells[3, 3].InheritedCellType; // コンボボックスとバインドしているコレクションにデータを追加します ItemDataCollection lst = ((ItemDataCollection)c.ItemsSource); lst.Add(new ItemData() { Text = "福岡", Value = "004" }); }
4.セル毎に項目を動的に変更する
セル毎に項目を動的に変更する場合、セル毎にコンボボックス型セルのインスタンスを作成する必要があります。
◎サンプルコード(VB)
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded ' ◆4. 項目を追加する Dim c6 As New GrapeCity.Windows.SpreadGrid.ComboBoxCellType() c6.ItemsSource = New ItemDataCollection() c6.ContentPath = "Text" c6.SelectedValuePath = "Value" gcSpreadGrid1.Cells(4, 4).CellType = c6 Dim c7 As New GrapeCity.Windows.SpreadGrid.ComboBoxCellType() c7.ItemsSource = New ItemDataCollection() c7.ContentPath = "Text" c7.SelectedValuePath = "Value" gcSpreadGrid1.Cells(4, 5).CellType = c7 End Sub Private Sub Button3_Click(sender As Object, e As RoutedEventArgs) Dim c As GrapeCity.Windows.SpreadGrid.ComboBoxCellType = DirectCast(gcSpreadGrid1.Cells(4, 4).InheritedCellType, GrapeCity.Windows.SpreadGrid.ComboBoxCellType) ' コンボボックスとバインドしているコレクションにデータを追加します Dim lst As ItemDataCollection = DirectCast(c.ItemsSource, ItemDataCollection) lst.Add(New ItemData() With { _ .Text = "福岡", _ .Value = "004" _ }) End Sub
◎サンプルコード(C#)
private void Window_Loaded(object sender, RoutedEventArgs e){ // ◆4. 項目を追加する GrapeCity.Windows.SpreadGrid.ComboBoxCellType c6 = new GrapeCity.Windows.SpreadGrid.ComboBoxCellType(); c6.ItemsSource = new ItemDataCollection(); c6.ContentPath = "Text"; c6.SelectedValuePath = "Value"; gcSpreadGrid1.Cells[4, 4].CellType = c6; GrapeCity.Windows.SpreadGrid.ComboBoxCellType c7 = new GrapeCity.Windows.SpreadGrid.ComboBoxCellType(); c7.ItemsSource = new ItemDataCollection(); c7.ContentPath = "Text"; c7.SelectedValuePath = "Value"; gcSpreadGrid1.Cells[4, 5].CellType = c7; } private void button3_Click(object sender, RoutedEventArgs e) { GrapeCity.Windows.SpreadGrid.ComboBoxCellType c = (GrapeCity.Windows.SpreadGrid.ComboBoxCellType)gcSpreadGrid1.Cells[4, 4].InheritedCellType; // コンボボックスとバインドしているコレクションにデータを追加します ItemDataCollection lst = ((ItemDataCollection)c.ItemsSource); lst.Add(new ItemData() { Text = "福岡", Value = "004" });}
【コンボボックスに連結しているクラス】
◎サンプルコード(VB)
Public Class ItemData Private _text As String Private _value As String Public Property Text() As String Get Return _text End Get Set(value As String) _text = Value End Set End Property Public Property Value() As String Get Return _value End Get Set(value As String) _value = Value End Set End Property Public Shared Function GetData() As List(Of ItemData) Dim list As New List(Of ItemData)() list.Add(New ItemData() With {.Text = "東京", .Value = "001"}) list.Add(New ItemData() With {.Text = "仙台", .Value = "002"}) list.Add(New ItemData() With {.Text = "大阪", .Value = "003"}) Return list End Function End Class Public Class ItemDataCollection Inherits ObservableCollection(Of ItemData) Public Sub New() Add(New ItemData() With {.Text = "東京", .Value = "001"}) Add(New ItemData() With {.Text = "仙台", .Value = "002"}) Add(New ItemData() With {.Text = "大阪", .Value = "003"}) End Sub End Class
◎サンプルコード(C#)
public class ItemData { private string _text; private string _value; public string Text { get { return _text; } set { _text = value; } } public string Value { get { return _value; } set { _value = value; } } public static ListGetData() { List list = new List (); list.Add(new ItemData() { Text = "東京", Value = "001" }); list.Add(new ItemData() { Text = "仙台", Value = "002" }); list.Add(new ItemData() { Text = "大阪", Value = "003" }); return list; } } public class ItemDataCollection : ObservableCollection { public ItemDataCollection() { Add(new ItemData() { Text = "東京", Value = "001" }); Add(new ItemData() { Text = "仙台", Value = "002" }); Add(new ItemData() { Text = "大阪", Value = "003" }); } }