Public Class Form Public dt As New DataTable Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Chk1 As New DataGridViewCheckBoxColumn Dim Txt6 As New DataGridViewTextBoxColumn dt.Columns.Add("選択") dt.Columns.Add("金額") Chk1.Name = "選択" Txt6.Name = "金額" DGV.Columns.Add(Chk1) DGV.Columns.Add(Txt6) DGV.Columns("選択").DataPropertyName = "選択" DGV.Columns("金額").DataPropertyName = "金額" DGV.Columns(0).ReadOnly = False '選択 DGV.Columns(6).ReadOnly = True '金額 DGV.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight DGV.Columns(6).DefaultCellStyle.Format = "#,0" DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells DGV.AllowUserToAddRows = False DGV.AllowUserToDeleteRows = False DGV.DataSource = dt DGV.AutoGenerateColumns = False 'Oracleに接続してDBアクセスしています。 '~略~ Dim i As String = rdr1.Item("AMOUNT") dt.Rows.Add(False, i ) '選択列にはデフォルトでFalseを入れる。 '~略~ UpdateSummaryLabel() End Sub Private Sub dgv_CellValueChanged(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) Handles DGV.CellValueChanged bs.EndEdit() UpdateSummaryLabel() End Sub Private Sub dgv_CurrentCellDirtyStateChanged(ByVal sender As Object, _ ByVal e As EventArgs) Handles DGV.CurrentCellDirtyStateChanged If DGV.CurrentCellAddress.X = 0 Then If DGV.IsCurrentCellDirty Then DGV.CommitEdit(DataGridViewDataErrorContexts.Commit) End If End If End Sub Private Sub dgv_DataError(ByVal sender As Object, _ ByVal e As DataGridViewDataErrorEventArgs) Handles DGV.DataError MessageBox.Show(e.Exception.Message, "入力エラー", _ MessageBoxButtons.OK, MessageBoxIcon.Information) e.ThrowException = False End Sub Private Sub UpdateSummaryLabel() Dim SumObject As Object = dt.Compute("Sum(金額)", "選択 = True") If IsDBNull(SumObject) Then Label.Text = "選択されていません。" Else Label.Text = CDec(SumObject).ToString("#,0") End If End Sub End Class