Public Class Form1 Private ds As DataSet Enum 消費税課税 As Integer 免税 = -1 原則課税 = 0 簡易課税 = 1 End Enum Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ds = New DataSet("さんぷる") ds.CaseSensitive = True ds.EnforceConstraints = False Dim dt1 As DataTable = Createクエリ1() Dim dt2 As DataTable = Create消費税課税() ds.Tables.Add(dt1) ds.Tables.Add(dt2) 'リレーションと式列を追加 ds.Relations.Add("Rel消費税課税", dt2.Columns("Key"), dt1.Columns("消費税課税"), True) dt1.Columns.Add("消費税課税と消費税申告料の結合", GetType(String), _ "Parent(Rel消費税課税).DisplayText+消費税申告料") ds.EnforceConstraints = True 'これはデザイン時に行っても OK InitGrid() End Sub Private Sub InitGrid() DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter DataGridView1.Columns.Clear() DataGridView1.AutoGenerateColumns = True DataGridView1.AllowUserToAddRows = False DataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False DataGridView1.RowHeadersVisible = False DataGridView1.AllowUserToResizeRows = False DataGridView1.DataSource = ds DataGridView1.DataMember = "クエリ1" DataGridView1.Columns("契約ID").Frozen = True DataGridView1.Columns("契約ID").DividerWidth = 3 DataGridView1.Columns("決算日").DefaultCellStyle.Format = "yyyy年MM月dd日" For Each col As String In "法人税申告料@消費税申告料@月次報酬".Split("@"c) With DataGridView1.Columns(col).DefaultCellStyle .Format = "N0" .Alignment = DataGridViewContentAlignment.MiddleRight End With Next DataGridView1.Columns("消費税課税").Visible = False DataGridView1.Columns("消費税申告料").Visible = False DataGridView1.Columns("月次報酬").DisplayIndex = 3 DataGridView1.Columns("摘要").DisplayIndex = 8 DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells) End Sub Private Function Createクエリ1() As DataTable Dim dt As New DataTable("クエリ1") Dim pkey As DataColumn = dt.Columns.Add("契約ID", GetType(Long)) pkey.ReadOnly = True pkey.Unique = True pkey.AutoIncrement = True pkey.AutoIncrementSeed = 10000 pkey.AutoIncrementStep = 1 dt.PrimaryKey = New DataColumn() {pkey} dt.Columns.Add("顧問先コード", GetType(Integer)) dt.Columns.Add("決算日", GetType(Date)).DateTimeMode = DataSetDateTime.Local dt.Columns.Add("消費税課税", GetType(消費税課税)) dt.Columns.Add("摘要", GetType(String)).AllowDBNull = True dt.Columns.Add("法人税申告料", GetType(Decimal)) dt.Columns.Add("消費税申告料", GetType(Decimal)) dt.Columns.Add("月次報酬", GetType(Decimal)) dt.Rows.Add(10000, 1200, #7/31/2018#, 消費税課税.原則課税, DBNull.Value, 98765D, 0D, 111111D) dt.Rows.Add(10001, 1201, #8/31/2018#, 消費税課税.原則課税, "適用", 98765D, 30000D, 222222D) dt.Rows.Add(10002, 1371, #9/30/2018#, 消費税課税.原則課税, "適当", 1000000, 20000D, 333333D) dt.Rows.Add(10003, 1390, #9/30/2018#, 消費税課税.免税, "敵よ", 1300000, 21000D, 43210D) dt.AcceptChanges() Return dt End Function Private Function Create消費税課税() As DataTable Dim dt As New DataTable("消費税課税") dt.PrimaryKey = New DataColumn() {dt.Columns.Add("Key", GetType(消費税課税))} dt.Columns.Add("DisplayText", GetType(String)) dt.Rows.Add(消費税課税.免税, "(免)") dt.Rows.Add(消費税課税.原則課税, "(原)") dt.Rows.Add(消費税課税.簡易課税, "(簡)") dt.AcceptChanges() Return dt End Function End Class