GcMultiRowにデータを表示するデータソースを取得または設定します。
public object DataSource {get; set;}
Public Property DataSource As Object
プロパティ値
GcMultiRowに表示するデータを含む
System.Object。既定値はnull 参照 (Visual Basicでは
Nothing)です。
次のサンプルコードは、データバインディングモード時のデータの管理方法を示します。ここでは、セルを特定のデータソース列にバインドする方法や、エラーの処理方法などを示しています。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Collections.Generic;
namespace GrapeCity.Win.MultiRow.SampleCode
{
public class DataBindingDemo : Form
{
private GcMultiRow gcMultiRow1 = new GcMultiRow();
public DataBindingDemo()
{
this.Text = "DataBinding Demo";
// Add MultiRow to form
this.gcMultiRow1.Dock = DockStyle.Fill;
this.Controls.Add(this.gcMultiRow1);
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeDataSource();
// The following to lines code can be instead by:
// this.gcMultiRow1.DataSource = studentInfoDataSet.Tables["BasicInfos"];
this.gcMultiRow1.DataSource = studentInfoDataSet;
this.gcMultiRow1.DataMember = "BasicInfos";
// When data binding, the cells in template should be set DataField property to binding a specific column of data source.
this.gcMultiRow1.Template = this.CreateDataBindingTamplate();
this.gcMultiRow1.DataError += new EventHandler<DataErrorEventArgs>(gcMultiRow1_DataError);
}
void gcMultiRow1_DataError(object sender, DataErrorEventArgs e)
{
// The first id cell only can input number, if user input some invalid value, DataError event will be fired.
// You should handle this event to handle some error cases.
if ((e.Context & DataErrorContexts.Commit) != 0)
{
// When committing value occurs error, show a massage box to notify user, and roll back value.
MessageBox.Show(e.Exception.Message);
EditingActions.CancelEdit.Execute(this.gcMultiRow1);
}
else
{
// Other handle.
}
}
Template CreateDataBindingTamplate()
{
TextBoxCell idCell = new TextBoxCell();
// Binding a column of data source.
// The value of DataField property should a exist column name in data source.
idCell.DataField = "ID";
TextBoxCell nameCell = new TextBoxCell();
nameCell.DataField = "Name";
CheckBoxCell genderCell = new CheckBoxCell();
genderCell.DataField = "Gender";
Cell[] cells = new Cell[] { idCell, nameCell, genderCell };
return Template.CreateGridTemplate(cells);
}
#region Initialize DataSource
DataSet studentInfoDataSet = new DataSet();
private void InitializeDataSource()
{
// For demonstration purpose, I construct a data source by code.
// You can connect a database, and create data table or data set by ADO.net technique instead.
// For more detail information about ADO.net, please refer to MSDN
studentInfoDataSet.Tables.Add("BasicInfos");
studentInfoDataSet.Tables.Add("Scores");
// Initialize BasicInfo table.
DataTable basicInfos = studentInfoDataSet.Tables["BasicInfos"];
// Initialize columns
basicInfos.Columns.Add("ID", typeof(int));
basicInfos.Columns.Add("Name", typeof(string));
basicInfos.Columns.Add("Gender", typeof(bool));
// Initialize data.
basicInfos.Rows.Add(0, "Jim", true);
basicInfos.Rows.Add(1, "Alice", false);
basicInfos.Rows.Add(2, "Tom", true);
// Omit Scores table's initializing logic here...
}
#endregion
[STAThreadAttribute()]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new DataBindingDemo());
}
}
}
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data
Imports GrapeCity.Win.MultiRow
Public Class DataBindingDemo
Inherits Form
Friend WithEvents gcMultiRow1 As New GcMultiRow()
Public Sub New()
Me.Text = "DataBinding Demo"
' Add MultiRow to form
Me.gcMultiRow1.Dock = DockStyle.Fill
Me.Controls.Add(Me.gcMultiRow1)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
InitializeDataSource()
' The following to lines code can be instead by:
' this.gcMultiRow1.DataSource = studentInfoDataSet.Tables["BasicInfos"];
Me.gcMultiRow1.DataSource = studentInfoDataSet
Me.gcMultiRow1.DataMember = "BasicInfos"
' When data binding, the cells in template should be set DataField property to binding a specific column of data source.
Me.gcMultiRow1.Template = Me.CreateDataBindingTamplate()
End Sub
Private Sub gcMultiRow1_DataError(ByVal sender As Object, ByVal e As DataErrorEventArgs) Handles gcMultiRow1.DataError
' The first id cell only can input number, if user input some invalid value, DataError event will be fired.
' You should handle this event to handle some error cases.
If (e.Context And DataErrorContexts.Commit) <> 0 Then
' When committing value occurs error, show a massage box to notify user, and roll back value.
MessageBox.Show(e.Exception.Message)
EditingActions.CancelEdit.Execute(Me.gcMultiRow1)
' Other handle.
Else
End If
End Sub
Private Function CreateDataBindingTamplate() As Template
Dim idCell As New TextBoxCell()
' Binding a column of data source.
' The value of DataField property should a exist column name in data source.
idCell.DataField = "ID"
Dim nameCell As New TextBoxCell()
nameCell.DataField = "Name"
Dim genderCell As New CheckBoxCell()
genderCell.DataField = "Sex"
Dim cells As Cell() = New Cell() {idCell, nameCell, genderCell}
Return Template.CreateGridTemplate(cells)
End Function
#Region "Initialize DataSource"
Private studentInfoDataSet As New DataSet()
Private Sub InitializeDataSource()
' For demonstration purpose, I construct a data source by code.
' You can connect a database, and create data table or data set by ADO.net technique instead.
' For more detail information about ADO.net, please refer to MSDN
studentInfoDataSet.Tables.Add("BasicInfos")
studentInfoDataSet.Tables.Add("Scores")
' Initialize BasicInfo table.
Dim basicInfos As DataTable = studentInfoDataSet.Tables("BasicInfos")
' Initialize columns
basicInfos.Columns.Add("ID", GetType(Integer))
basicInfos.Columns.Add("Name", GetType(String))
basicInfos.Columns.Add("Gender", GetType(Boolean))
' Initialize data.
basicInfos.Rows.Add(0, "Jim", True)
basicInfos.Rows.Add(1, "Alice", False)
basicInfos.Rows.Add(2, "Tom", True)
' Omit Scores table's initializing logic here...
End Sub
#End Region
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New DataBindingDemo())
End Sub
End Class