コントロールがデータソースに連結されている場合、データソースに非連結な列を追加、および任意の位置に挿入できます。
上の図の「金額」列は非連結列です。「金額」列の値は「在庫数」と「単価」の積により得られるため、データソースに保存する必要のない情報です。
コントロールに非連結列を追加/挿入するには次の2つの方法があります。
それぞれの方法について説明します。
データ連結による列の自動生成で連結列を生成し、非連結列を追加または挿入します。
次のサンプルコードはデータ連結により列を自動生成してから 10 列目に非連結な「TotalPrice」列を挿入します。
C# |
コードのコピー |
---|---|
gcSpreadGrid1.ItemsSource = new StockCollection(); // 非連結列の挿入 gcSpreadGrid1.Columns.Insert(9); gcSpreadGrid1.Columns[9].Name = "TotalPrice"; gcSpreadGrid1.Columns["TotalPrice"].Formula = "UnitPrice * Quantity"; gcSpreadGrid1.Columns[9].Header = "金額"; |
Visual Basic |
コードのコピー |
---|---|
GcSpreadGrid1.ItemsSource = New StockCollection() ' 非連結列の挿入 GcSpreadGrid1.Columns.Insert(9) GcSpreadGrid1.Columns(9).Name = "TotalPrice" GcSpreadGrid1.Columns("TotalPrice").Formula = "UnitPrice * Quantity" GcSpreadGrid1.Columns(9).Header = "金額" |
C# |
コードのコピー |
---|---|
public class Stock : INotifyPropertyChanged { string productcode; string productname; DateTime stockdate; string noteno; DateTime expiredate; string lotno; int quantity; string term; double unitprice; string section; [Display(ShortName = "商品コード")] public string ProductCode { set { productcode = value; NotifyPropertyChanged("ProductCode"); } get { return productcode; } } [Display(ShortName = "商品名")] public string ProductName { set { productname = value; NotifyPropertyChanged("ProductName"); } get { return productname; } } [Display(ShortName = "入力日付")] public DateTime StockDate { set { stockdate = value; NotifyPropertyChanged("StockDate"); } get { return stockdate; } } [Display(ShortName = "伝票番号")] public string NoteNo { set { noteno = value; NotifyPropertyChanged("NoteNo"); } get { return noteno; } } [Display(ShortName = "消費期限")] public DateTime ExpireDate { set { expiredate = value; NotifyPropertyChanged("ExpireDate"); } get { return expiredate; } } [Display(ShortName = "ロットNo.")] public string LotNo { set { lotno = value; NotifyPropertyChanged("LotNo"); } get { return lotno; } } [Display(ShortName = "在庫数")] public int Quantity { set { quantity = value; NotifyPropertyChanged("Quantity"); } get { return quantity; } } [Display(ShortName = "単位")] public string Term { set { term = value; NotifyPropertyChanged("Term"); } get { return term; } } [Display(ShortName = "単価")] public double UnitPrice { set { unitprice = value; NotifyPropertyChanged("UnitPrice"); } get { return unitprice; } } [Display(ShortName = "区分")] public string Section { set { section = value; NotifyPropertyChanged("Section"); } get { return section; } } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class StockCollection : ObservableCollection<Stock> { public StockCollection() { Add(new Stock { ProductCode = "0000000", ProductName = "スイートアーモンド", StockDate = DateTime.Today.AddDays(-36), NoteNo = "D040000004", ExpireDate = DateTime.Today.AddDays(28), LotNo = "2011092001", Quantity = 200, Term = "本", UnitPrice = 90, Section = "仕入" }); Add(new Stock { ProductCode = "0000000", ProductName = "スイートアーモンド", StockDate = DateTime.Today.AddDays(-36), NoteNo = "D040000004", ExpireDate = DateTime.Today.AddDays(28), LotNo = "2011092003", Quantity = 400, Term = "本", UnitPrice = 90, Section = "仕入" }); Add(new Stock { ProductCode = "0000000", ProductName = "スイートアーモンド", StockDate = DateTime.Today.AddDays(-26), NoteNo = "D040000001", ExpireDate = DateTime.Today.AddDays(38), LotNo = "0000001", Quantity = 490, Term = "本", UnitPrice = 90, Section = "仕入" }); Add(new Stock { ProductCode = "0000001", ProductName = "グレープシード", StockDate = DateTime.Today.AddDays(-28), NoteNo = "D040000007", ExpireDate = DateTime.Today.AddDays(25), LotNo = "20111119001", Quantity = 10, Term = "本", UnitPrice = 120, Section = "仕入" }); Add(new Stock { ProductCode = "0000001", ProductName = "グレープシード", StockDate = DateTime.Today.AddDays(-25), NoteNo = "M040000002", ExpireDate = DateTime.Today.AddDays(28), LotNo = "123", Quantity = 4, Term = "本", UnitPrice = 100, Section = "製造" }); Add(new Stock { ProductCode = "0000003", ProductName = "オリーブ", StockDate = DateTime.Today.AddDays(-28), NoteNo = "D040000006", ExpireDate = DateTime.Today.AddDays(40), LotNo = "20111119010", Quantity = 9, Term = "本", UnitPrice = 2000, Section = "仕入" }); Add(new Stock { ProductCode = "0000005", ProductName = "馬油", StockDate = DateTime.Today.AddDays(-15), NoteNo = "D040000009", ExpireDate = DateTime.Today.AddDays(45), LotNo = "20111214002", Quantity = 84, Term = "Kg", UnitPrice = 3200, Section = "仕入" }); Add(new Stock { ProductCode = "0000006", ProductName = "ビーワックス", StockDate = DateTime.Today.AddDays(-12), NoteNo = "D040000011", ExpireDate = DateTime.Today.AddDays(60), LotNo = "20111217004", Quantity = 150, Term = "Kg", UnitPrice = 1500, Section = "仕入" }); Add(new Stock { ProductCode = "0000010", ProductName = "えごま", StockDate = DateTime.Today.AddDays(-3), NoteNo = "D040000008", ExpireDate = DateTime.Today.AddDays(90), LotNo = "20111120002", Quantity = 52, Term = "本", UnitPrice = 120, Section = "仕入" }); } } |
Visual Basic |
コードのコピー |
---|---|
Public Class Stock Implements INotifyPropertyChanged Private m_productcode As String Private m_productname As String Private m_stockdate As DateTime Private m_noteno As String Private m_expiredate As DateTime Private m_lotno As String Private m_quantity As Integer Private m_term As String Private m_unitprice As Double Private m_section As String <Display(ShortName:="商品コード", Description:="商品コードです")> _ <Editable(False)> _ Public Property ProductCode() As String Get Return m_productcode End Get Set(value As String) m_productcode = value NotifyPropertyChanged("ProductCode") End Set End Property <Display(ShortName:="商品名")> _ Public Property ProductName() As String Get Return m_productname End Get Set(value As String) m_productname = value NotifyPropertyChanged("ProductName") End Set End Property <Display(ShortName:="入力日付")> _ Public Property StockDate() As DateTime Get Return m_stockdate End Get Set(value As DateTime) m_stockdate = value NotifyPropertyChanged("StockDate") End Set End Property <Display(ShortName:="伝票番号")> _ Public Property NoteNo() As String Get Return m_noteno End Get Set(value As String) m_noteno = value NotifyPropertyChanged("NoteNo") End Set End Property <Display(ShortName:="消費期限")> _ Public Property ExpireDate() As DateTime Get Return m_expiredate End Get Set(value As DateTime) m_expiredate = value NotifyPropertyChanged("ExpireDate") End Set End Property <Display(ShortName:="ロットNo.")> _ Public Property LotNo() As String Get Return m_lotno End Get Set(value As String) m_lotno = value NotifyPropertyChanged("LotNo") End Set End Property <Display(ShortName:="在庫数")> _ Public Property Quantity() As Integer Get Return m_quantity End Get Set(value As Integer) m_quantity = value NotifyPropertyChanged("Quantity") End Set End Property <Display(ShortName:="単位")> _ Public Property Term() As String Get Return m_term End Get Set(value As String) m_term = value NotifyPropertyChanged("Term") End Set End Property <Display(ShortName:="単価")> _ Public Property UnitPrice() As Double Get Return m_unitprice End Get Set(value As Double) m_unitprice = value NotifyPropertyChanged("UnitPrice") End Set End Property <Display(ShortName:="区分")> _ Public Property Section() As String Get Return m_section End Get Set(value As String) m_section = value NotifyPropertyChanged("Section") End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Protected Sub NotifyPropertyChanged(propertyName As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) End Sub End Class Public Class StockCollection Inherits ObservableCollection(Of Stock) Public Sub New() Add(New Stock() With { _ .ProductCode = "0000000", _ .ProductName = "スイートアーモンド", _ .StockDate = DateTime.Today.AddDays(-36), _ .NoteNo = "D040000004", _ .ExpireDate = DateTime.Today.AddDays(28), _ .LotNo = "2011092001", _ .Quantity = 200, _ .Term = "本", _ .UnitPrice = 90, _ .Section = "仕入" _ }) Add(New Stock() With { _ .ProductCode = "0000000", _ .ProductName = "スイートアーモンド", _ .StockDate = DateTime.Today.AddDays(-36), _ .NoteNo = "D040000004", _ .ExpireDate = DateTime.Today.AddDays(28), _ .LotNo = "2011092003", _ .Quantity = 400, _ .Term = "本", _ .UnitPrice = 90, _ .Section = "仕入" _ }) Add(New Stock() With { _ .ProductCode = "0000000", _ .ProductName = "スイートアーモンド", _ .StockDate = DateTime.Today.AddDays(-26), _ .NoteNo = "D040000001", _ .ExpireDate = DateTime.Today.AddDays(38), _ .LotNo = "0000001", _ .Quantity = 490, _ .Term = "本", _ .UnitPrice = 90, _ .Section = "仕入" _ }) Add(New Stock() With { _ .ProductCode = "0000001", _ .ProductName = "グレープシード", _ .StockDate = DateTime.Today.AddDays(-28), _ .NoteNo = "D040000007", _ .ExpireDate = DateTime.Today.AddDays(25), _ .LotNo = "20111119001", _ .Quantity = 10, _ .Term = "本", _ .UnitPrice = 120, _ .Section = "仕入" _ }) Add(New Stock() With { _ .ProductCode = "0000001", _ .ProductName = "グレープシード", _ .StockDate = DateTime.Today.AddDays(-25), _ .NoteNo = "M040000002", _ .ExpireDate = DateTime.Today.AddDays(28), _ .LotNo = "123", _ .Quantity = 4, _ .Term = "本", _ .UnitPrice = 100, _ .Section = "製造" _ }) Add(New Stock() With { _ .ProductCode = "0000003", _ .ProductName = "オリーブ", _ .StockDate = DateTime.Today.AddDays(-28), _ .NoteNo = "D040000006", _ .ExpireDate = DateTime.Today.AddDays(40), _ .LotNo = "20111119010", _ .Quantity = 9, _ .Term = "本", _ .UnitPrice = 2000, _ .Section = "仕入" _ }) Add(New Stock() With { _ .ProductCode = "0000005", _ .ProductName = "馬油", _ .StockDate = DateTime.Today.AddDays(-15), _ .NoteNo = "D040000009", _ .ExpireDate = DateTime.Today.AddDays(45), _ .LotNo = "20111214002", _ .Quantity = 84, _ .Term = "Kg", _ .UnitPrice = 3200, _ .Section = "仕入" _ }) Add(New Stock() With { _ .ProductCode = "0000006", _ .ProductName = "ビーワックス", _ .StockDate = DateTime.Today.AddDays(-12), _ .NoteNo = "D040000011", _ .ExpireDate = DateTime.Today.AddDays(60), _ .LotNo = "20111217004", _ .Quantity = 150, _ .Term = "Kg", _ .UnitPrice = 1500, _ .Section = "仕入" _ }) Add(New Stock() With { _ .ProductCode = "0000010", _ .ProductName = "えごま", _ .StockDate = DateTime.Today.AddDays(-3), _ .NoteNo = "D040000008", _ .ExpireDate = DateTime.Today.AddDays(90), _ .LotNo = "20111120002", _ .Quantity = 52, _ .Term = "本", _ .UnitPrice = 120, _ .Section = "仕入" _ }) End Sub End Class |
データ連結による列の自動生成を無効化し、連結列および非連結列をそれぞれ設定します。
次のサンプルコードは列の自動生成を無効化し、連結列および非連結列をそれぞれ設定します。このサンプルコードではコントロールを StockCollection に連結しています。Stock および StockCollection クラスの完全なコードは、上のサンプルコードを参照してください。
C# |
コードのコピー |
---|---|
gcSpreadGrid1.AutoGenerateColumns = false; gcSpreadGrid1.ItemsSource = new StockCollection(); gcSpreadGrid1.ColumnCount = 11; // 連結列の設定 gcSpreadGrid1.Columns[0].DataField = new PropertyDataField() { Property = "ProductCode" }; gcSpreadGrid1.Columns[1].DataField = new PropertyDataField() { Property = "ProductName" }; gcSpreadGrid1.Columns[2].DataField = new PropertyDataField() { Property = "StockDate" }; gcSpreadGrid1.Columns[3].DataField = new PropertyDataField() { Property = "NoteNo" }; gcSpreadGrid1.Columns[4].DataField = new PropertyDataField() { Property = "ExpireDate" }; gcSpreadGrid1.Columns[5].DataField = new PropertyDataField() { Property = "LotNo" }; gcSpreadGrid1.Columns[6].DataField = new PropertyDataField() { Property = "Quantity" }; gcSpreadGrid1.Columns[7].DataField = new PropertyDataField() { Property = "Term" }; gcSpreadGrid1.Columns[8].DataField = new PropertyDataField() { Property = "UnitPrice" }; gcSpreadGrid1.Columns[10].DataField = new PropertyDataField() { Property = "Section" }; // 非連結列の設定 gcSpreadGrid1.Columns[9].Name = "TotalPrice"; gcSpreadGrid1.Columns["TotalPrice"].Formula = "UnitPrice * Quantity"; gcSpreadGrid1.Columns[9].Header = "金額"; |
Visual Basic |
コードのコピー |
---|---|
GcSpreadGrid1.AutoGenerateColumns = False GcSpreadGrid1.ItemsSource = New StockCollection() GcSpreadGrid1.ColumnCount = 11 ' 連結列の設定 GcSpreadGrid1.Columns(0).DataField = New PropertyDataField() With {.Property = "ProductCode"} GcSpreadGrid1.Columns(1).DataField = New PropertyDataField() With {.Property = "ProductName"} GcSpreadGrid1.Columns(2).DataField = New PropertyDataField() With {.Property = "StockDate"} GcSpreadGrid1.Columns(3).DataField = New PropertyDataField() With {.Property = "NoteNo"} GcSpreadGrid1.Columns(4).DataField = New PropertyDataField() With {.Property = "ExpireDate"} GcSpreadGrid1.Columns(5).DataField = New PropertyDataField() With {.Property = "LotNo"} GcSpreadGrid1.Columns(6).DataField = New PropertyDataField() With {.Property = "Quantity"} GcSpreadGrid1.Columns(7).DataField = New PropertyDataField() With {.Property = "Term"} GcSpreadGrid1.Columns(8).DataField = New PropertyDataField() With {.Property = "UnitPrice"} GcSpreadGrid1.Columns(10).DataField = New PropertyDataField() With {.Property = "Section"} ' 非連結列の設定 GcSpreadGrid1.Columns(9).Name = "TotalPrice" GcSpreadGrid1.Columns("TotalPrice").Formula = "UnitPrice * Quantity" GcSpreadGrid1.Columns(9).Header = "金額" |