| 
                 分類:[.NET 全般] 
 
 ここに初めて書き込みをさせていただきます。
vb.net初心者です。
今、私はdatagridviewで単価、数量の列を入力して、enter押して金額、テキストボックス(合計、消費税)が自動計算されるように作成しています。
作成してて問題点が・・・・・。
@enterでセルを横移動するようにしています。空白のままだと横移動して数量のところでだったら新しい行を追加して単価に戻るのですが、単価を入力してenterを押すと新しい行を追加して下の行に移ってしまいます。
A単価と数量を入力して値を変えてセルが離れたときに合計、テキストボックス(合計、消費税)が変わるようにしたいのですが、今は数量のときにしか変わらないようになっている・・・・・。
B単価、数量、合計、テキストボックス(合計、消費税)の桁数制限をしたい。「DataGridView1TextBox_KeyPress」で処理を書こうと思っていますが、どう書けばいいかわからないです・・・・。単価3桁、数量3桁、合計8桁(カンマ区切り。3桁ごと)。テキストボックスは合計はカンマ処理していますが、桁数制限をしていません。8桁です。消費税の方も8桁で制限しないといけませんが、カンマ編集のやり方がわかりません。消費税は小数点も含まれてくるのでそのあたりが・・・・・・。
CVB.net2008です。
windowsXPです。
Dお忙しいとは思いますが、どうかお知恵を拝借できませんでしょうか・・・・。お願いします。もうこれで三日間悩み続けてます。
ソースをはっておきます。
みにくいソースですいませんでした。
Public Class Form1
    'テーブル宣言
    Private m_dt As DataTable
    '行取得用の変数宣言
    Private currentRow As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_dt = New System.Data.DataTable
        m_dt.Columns.Add("単価", Type.GetType("System.String"))
        m_dt.Columns.Add("数量", Type.GetType("System.String"))
        m_dt.Columns.Add("合計", Type.GetType("System.String"))
    End Sub
    'セルの編集が終了したら
    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Call cellendedit()
    End Sub
    '金額、合計,消費税の自動計算
    Private Sub cellendedit()
        '単価、数量,合計、消費税の結果を入れるための変数
        Dim tanka As Integer
        Dim suuryou As Integer
        Dim total As Integer
        Dim syouhize As Double
        Dim cell As String = DataGridView1.CurrentCell.Value
        currentRow = DataGridView1.CurrentCell.RowIndex
        Select Case DataGridView1.CurrentCell.OwningColumn.Name
            Case "数量"
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    tanka = CInt(DataGridView1.Rows(i).Cells(0).Value)
                    suuryou = suuryou + CInt(DataGridView1.Rows(i).Cells(1).Value)
                    'これだとカンマ編集してない。金額のセルにはそのままの値が入る
                    DataGridView1.Rows(i).Cells(2).Value = CInt(DataGridView1.Rows(i).Cells(0).Value) + CInt(DataGridView1.Rows(i).Cells(1).Value)
                    total = total + DataGridView1.Rows(i).Cells(2).Value
                Next
                textSumGoukei.Text = total.ToString("#,###,#")
                syouhize = total * 1.05 / 100.05
                '整数、小数点あわせて9桁になるようにしたい。
                'これだとならない。
                txtSyouhizei.Text = syouhize.ToString
        End Select
    End Sub
    'イベントハンドラ
    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
            Dim dgv As DataGridView = CType(sender, DataGridView)
            Dim tb As DataGridViewTextBoxEditingControl = CType(e.Control, DataGridViewTextBoxEditingControl)
            RemoveHandler tb.KeyPress, AddressOf DataGridView1TextBox_KeyPress
            AddHandler tb.KeyPress, AddressOf DataGridView1TextBox_KeyPress
        End If
    End Sub
    'セル移動。ただしこれだと単価を入力してenterを押すと新しい行が追加されてしまう・・・・。
       Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        currentRow = DataGridView1.CurrentCell.RowIndex
        Dim curentColum As Integer = DataGridView1.CurrentCell.ColumnIndex
        Dim nextColum As Integer = curentColum + 1
        Dim nextrow As Integer = currentRow
        If e.KeyCode = Keys.Enter Then
            If nextColum = 2 Then
                nextColum = 0
                nextrow += 1
                DataGridView1.Rows.Add()
            End If
            DataGridView1.CurrentCell = DataGridView1(nextColum, nextrow)
        End If
    End Sub
    '入力文字制限。ここに入力文字数制限もしたい。
    Private Sub DataGridView1TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
        Select Case DataGridView1.CurrentCell.OwningColumn.Name
            Case "単価"
                Select Case e.KeyChar
                    Case "0" To "9"
                    Case Microsoft.VisualBasic.Chr(Keys.Back)
                    Case Else
                        e.KeyChar = Nothing
                End Select
            Case "数量"
                Select Case e.KeyChar
                    Case "0" To "9"
                    Case Microsoft.VisualBasic.Chr(Keys.Back)
                    Case Else
                        e.KeyChar = Nothing
                End Select
        End Select
    End Sub
End Class
以上です。
  |