SPREAD for WPF 3.0J - GcSpreadSheet > 開発者ガイド > データ連結 |
GcSpreadSheetは、データ連結を対応し、最小限のコードでワークシートにてデータを挿入できます。ワークシートを以下に記載されている各々のデータソースと連結することが可能です。
次の手順でワークシートをデータテーブルと連結できます。
コードのコピー
|
|
---|---|
// データテーブルを作成します。 public DataTable GetTable() { DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Age"); DataRow dr = dt.NewRow(); dr.ItemArray = new object[] { "Jack", 35 }; dt.Rows.Add(dr); dr = dt.NewRow(); dr.ItemArray = new object[] { "Jimmy", 42 }; dt.Rows.Add(dr); dr = dt.NewRow(); dr.ItemArray = new object[] { "Sam", 24 }; dt.Rows.Add(dr); dr = dt.NewRow(); dr.ItemArray = new object[] { "Venka", 55 }; dt.Rows.Add(dr); return dt; } |
コードのコピー
|
|
---|---|
'データテーブルを作成します。 Public Function GetTable() As DataTable Dim dt As DataTable = New DataTable() dt.Columns.Add("Name") dt.Columns.Add("Age") Dim dr As DataRow = dt.NewRow() dr.ItemArray = New Object() {"Jack", 35} dt.Rows.Add(dr) dr = dt.NewRow() dr.ItemArray = New Object() {"Jimmy", 42} dt.Rows.Add(dr) dr = dt.NewRow() dr.ItemArray = New Object() {"Sam", 24} dt.Rows.Add(dr) dr = dt.NewRow() dr.ItemArray = New Object() {"Venka", 55} dt.Rows.Add(dr) Return dt End Function |
コードのコピー
|
|
---|---|
// DataTable // Sheet1のDataTableを連結します。 DataTable dt = GetTable(); gcSpreadSheet.Workbook.Worksheets[0].DataManager.DataSource = dt; gcSpreadSheet.Workbook.Worksheets[0].Name = "DataTable"; |
コードのコピー
|
|
---|---|
'DataTable 'Sheet1のDataTableを連結します。 Dim dt As DataTable = GetTable() GcSpreadsheet.Workbook.Worksheets(0).DataManager.DataSource = dt GcSpreadsheet.Workbook.Worksheets(0).Name = "DataTable" |
データテーブル以外、ワークシートのデータソースをデータビューに設定できます。ワークシートをデータビューと連結するには、データビューを作成してDataSourceプロパティをデータセットから作成したデータビューに設定します。
コードのコピー
|
|
---|---|
// DataView string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Nwind.mdb"; OleDbConnection conn = new OleDbConnection(connString); OleDbCommand comm = new OleDbCommand("Select * from Products", conn); OleDbDataAdapter adap = new OleDbDataAdapter(comm); DataSet ds = new DataSet(); adap.Fill(ds); // Sheet2のDataViewを連結します。 DataView dv = ds.Tables[0].DefaultView; gcSpreadSheet.Workbook.Worksheets[1].DataManager.DataSource = dv; gcSpreadSheet.Workbook.Worksheets[1].Name = "DataView"; |
コードのコピー
|
|
---|---|
'DataView Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Nwind.mdb" Dim conn As OleDbConnection = New OleDbConnection(connString) Dim comm As OleDbCommand = New OleDbCommand("Select * from Products", conn) Dim adap As OleDbDataAdapter = New OleDbDataAdapter(comm) Dim ds As DataSet = New DataSet() adap.Fill(ds) 'Sheet2のDataViewを連結します。 Dim dv As DataView = ds.Tables(0).DefaultView GcSpreadsheet.Workbook.Worksheets(1).DataManager.DataSource = dv GcSpreadsheet.Workbook.Worksheets(1).Name = "DataView" |
次の手順でワークシートをCollectionViewと連結できます。
コードのコピー
|
|
---|---|
public class Widget { public Widget(string name, double marketValue, long magnitude) { Name = name; MarketValue = marketValue; Magnitude = magnitude; } private string name; private double marketValue; private long magnitude; public string Name { get { return this.name; } set { this.name = value; } } public double MarketValue { get { return this.marketValue; } set { this.marketValue = value; } } public long Magnitude { get { return this.magnitude; } set { this.magnitude = value; } } } public class MyCollectionView : CollectionBase, IBindingList { public MyCollectionView() { } public event ListChangedEventHandler ListChanged; public object AddNew() { return this.AddNew("New Widget", 0, 0); } public Widget AddNew(string name, double marketValue, long magnitude) { Widget w = new Widget(name, marketValue, magnitude); this.List.Add(w); return w; } public void AddIndex(PropertyDescriptor property) { } public bool AllowEdit { get { return true; } } public bool AllowNew { get { return true; } } public bool AllowRemove { get { return true; } } public void ApplySort(PropertyDescriptor property, ListSortDirection direction) { } public int Find(PropertyDescriptor property, object key) { return 0; } public bool IsSorted { get { return true; } } public void RemoveIndex(PropertyDescriptor property) { } public void RemoveSort() { } public ListSortDirection SortDirection { get { return new ListSortDirection(); } } public PropertyDescriptor SortProperty { get { return null; } } public bool SupportsChangeNotification { get { return true; } } public bool SupportsSearching { get { return true; } } public bool SupportsSorting { get { return true; } } public Widget this[int index] { get { return this.List[index] as Widget; } set { this.List[index] = value; } } } |
コードのコピー
|
|
---|---|
Public Class Widget
Public Sub New(ByVal name As String, ByVal marketValue As Double, ByVal magnitude As Long)
Name = name
MarketValue = marketValue
Magnitude = magnitude
End Sub
Private name As String
Private marketValue As Double
Private magnitude As Long
Public Property Name As String
Get
Return Me.name
End Get
Set(ByVal value As String)
Me.name = value
End Set
End Property
Public Property MarketValue As Double
Get
Return Me.marketValue
End Get
Set(ByVal value As Double)
Me.marketValue = value
End Set
End Property
Public Property Magnitude As Long
Get
Return Me.magnitude
End Get
Set(ByVal value As Long)
Me.magnitude = value
End Set
End Property
End Class
Public Class MyCollectionView
Inherits CollectionBase
Implements IBindingList
Public Sub New()
End Sub
Public Event ListChanged As ListChangedEventHandler
Public Function AddNew() As Object
Return Me.AddNew("New Widget", 0, 0)
End Function
Public Function AddNew(ByVal name As String, ByVal marketValue As Double, ByVal magnitude As Long) As Widget
Dim w As Widget = New Widget(name, marketValue, magnitude)
Me.List.Add(w)
Return w
End Function
Public Sub AddIndex(ByVal [property] As PropertyDescriptor)
End Sub
Public ReadOnly Property AllowEdit As Boolean
Get
Return True
End Get
End Property
Public ReadOnly Property AllowNew As Boolean
Get
Return True
End Get
End Property
Public ReadOnly Property AllowRemove As Boolean
Get
Return True
End Get
End Property
Public Sub ApplySort(ByVal [property] As PropertyDescriptor, ByVal direction As ListSortDirection)
End Sub
Public Function Find(ByVal [property] As PropertyDescriptor, ByVal key As Object) As Integer
Return 0
End Function
Public ReadOnly Property IsSorted As Boolean
Get
Return True
End Get
End Property
Public Sub RemoveIndex(ByVal [property] As PropertyDescriptor)
End Sub
Public Sub RemoveSort()
End Sub
Public ReadOnly Property SortDirection As ListSortDirection
Get
Return New ListSortDirection()
End Get
End Property
Public ReadOnly Property SortProperty As PropertyDescriptor
Get
Return Nothing
End Get
End Property
Public ReadOnly Property SupportsChangeNotification As Boolean
Get
Return True
End Get
End Property
Public ReadOnly Property SupportsSearching As Boolean
Get
Return True
End Get
End Property
Public ReadOnly Property SupportsSorting As Boolean
Get
Return True
End Get
End Property
Default Public Property Item(ByVal index As Integer) As Widget
Get
Return TryCast(Me.List(index), Widget)
End Get
Set(ByVal value As Widget)
Me.List(index) = value
End Set
End Property
End Class
|
コードのコピー
|
|
---|---|
// データをCollectionViewに追加します。 MyCollectionView cv = new MyCollectionView(); cv.AddNew("Average Widget", 700.15, 300); cv.AddNew("Super Widget", 1500.7, 432); cv.AddNew("Super Fantastic Widget", 1700.5, 500); cv.AddNew("Messiah Widget", 2000.00, 600); |
コードのコピー
|
|
---|---|
' データをCollectionViewに追加します。 Dim cv As MyCollectionView = New MyCollectionView() cv.AddNew("Average Widget", 700.15, 300) cv.AddNew("Super Widget", 1500.7, 432) cv.AddNew("Super Fantastic Widget", 1700.5, 500) cv.AddNew("Messiah Widget", 2000.00, 600) |
コードのコピー
|
|
---|---|
// Sheet3でAutoGenerateColumnsをtrueに設定してCollectionViewを連結します。 gcSpreadSheet.Workbook.Worksheets[2].DataManager.DataSource = CollectionViewSource.GetDefaultView(cv); gcSpreadSheet.Workbook.Worksheets[2].Name = "CV AutoGenerateColumns True"; gcSpreadSheet.Workbook.Worksheets[2].Columns[0, 2].ColumnWidth = 140; |
コードのコピー
|
|
---|---|
' Sheet3でAutoGenerateColumnsをtrueに設定してCollectionViewを連結します。 Private Sub SurroundingSub() gcSpreadSheet.Workbook.Worksheets(2).DataManager.DataSource = CollectionViewSource.GetDefaultView(cv) gcSpreadSheet.Workbook.Worksheets(2).Name = "CV AutoGenerateColumns True" gcSpreadSheet.Workbook.Worksheets(2).Columns(0, 2).ColumnWidth = 140 End Sub |
コードのコピー
|
|
---|---|
// Sheet4でAutoGenerateColumnsをfalseに設定してCollectionViewを連結します。 gcSpreadSheet.Workbook.Worksheets[3].DataManager.AutoGenerateColumns = false; gcSpreadSheet.Workbook.Worksheets[3].DataManager.SetModelDataColumn(0, "Name"); gcSpreadSheet.Workbook.Worksheets[3].DataManager.SetModelDataColumn(1, "MarketValue"); gcSpreadSheet.Workbook.Worksheets[3].DataManager.SetModelDataColumn(2, "Magnitude"); gcSpreadSheet.Workbook.Worksheets[3].DataManager.DataSource = CollectionViewSource.GetDefaultView(cv); gcSpreadSheet.Workbook.Worksheets[3].Name = "CV AutoGenerateColumns False"; gcSpreadSheet.Workbook.Worksheets[3].Columns[0, 2].ColumnWidth = 140; |
コードのコピー
|
|
---|---|
' Sheet4でAutoGenerateColumnsをfalseに設定してCollectionViewを連結します。 Private Sub SurroundingSub() gcSpreadSheet.Workbook.Worksheets(3).DataManager.AutoGenerateColumns = False gcSpreadSheet.Workbook.Worksheets(3).DataManager.SetModelDataColumn(0, "Name") gcSpreadSheet.Workbook.Worksheets(3).DataManager.SetModelDataColumn(1, "MarketValue") gcSpreadSheet.Workbook.Worksheets(3).DataManager.SetModelDataColumn(2, "Magnitude") gcSpreadSheet.Workbook.Worksheets(3).DataManager.DataSource = CollectionViewSource.GetDefaultView(cv) gcSpreadSheet.Workbook.Worksheets(3).Name = "CV AutoGenerateColumns False" gcSpreadSheet.Workbook.Worksheets(3).Columns(0, 2).ColumnWidth = 140 End Sub |