開発環境:WinXP(SP3) VB2005 Oracle9i
あるテーブルのデータをDataGridViewに表示して
編集するものを作成しています。
そこでDataGridView編集時に問題が発生しました。
問題:「.」を入力したらCellValueChangedイベントが走り
編集中のセル値=0で計算処理が行われる。
DataGridView列: 単価、管理費、金額
単価もしくは管理費を変更したら単価+管理費を金額に入れます。
例えば 単価=3.9 管理費=1 金額=4.9
と入っている単価を4.9に変更しようとすると
4.を入力した段階で金額が1になります
(金額の計算はCellValueChangedイベント内で計算しています。)
4.9を入力してからCellValueChangedイベントが走る方法か
4.を入力したら4+1=5で金額に5を入れる方法をご教授願います。
不足情報、条件等がございましたらご指摘のほどお願いします。
これでどうでしょう。
Public Class Form1
Private table As DataTable
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
table = New DataTable()
table.Columns.Add("単価", GetType(Decimal))
table.Columns.Add("管理費", GetType(Decimal))
table.Columns.Add("金額", GetType(Decimal), "単価+管理費")
table.Rows.Add(3.9D, 1D)
table.Rows.Add(2.3D, 2D)
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
End Sub
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
BindingSource1.EndEdit()
End Sub
End Class
魔界の仮面弁士さん
サンプルコードありがとうございました。
DataGridView の EditMode を EditOnEnter にすることで
解決しました。
いつも具体的で分かりやすい説明&サンプルコードありがとうございます。