讀者問題與解答 - 累計結餘介面(類似試算表)

摘要:讀者問題與解答 - 累計結餘介面(類似試算表)


圖表 1 

Hello,呆呆,我想圖表 1 所示,利用 DataGridView 控制項所製作出來的累計結餘介面(類似試算表)應該就是你所要的我們已經將刪除資料列納入考慮。程式碼如下所列,我就不再一一解釋,請自行參考: 

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form3
    Inherits Form

    Private WithEvents DataGridView1 As New DataGridView()

   
款項說明"
            .Columns(1).Name = "支出"
            .Columns(2).Name = "存入"

            .Columns(3).Name = "累計結餘"
            .Columns("款項說明").HeaderText = "款項說明"
            .Columns("支出").HeaderText = "支出(請輸入負值)"
            .Columns("存入").HeaderText = "存入"
            .Columns("累計結餘").HeaderText = "累計結餘"
            .Columns("累計結餘").ReadOnly = True
            .Columns("
款項說明").SortMode = _
                DataGridViewColumnSortMode.NotSortable
            .Columns("
支出").SortMode = _
                DataGridViewColumnSortMode.NotSortable
            .Columns("
存入").SortMode = _
                DataGridViewColumnSortMode.NotSortable
            .Columns("
累計結餘").SortMode = _
                DataGridViewColumnSortMode.NotSortable
        End With

        With Me.DataGridView1.Rows
            .Add(New String() {"
起始累計結餘", "", "", "1000"})
            .Add(New String() {"
股息收入", "", "850", ""})
            .Add(New String() {"
房租", "-500", "", ""})
            .Add(New String() {"
汽車保養費", "-25", "", ""})
            .Add(New String() {"
退稅", "", "300", ""})
        End With

        '
允許使用者編輯起始累計結餘儲存格
        DataGridView1.Rows(0).ReadOnly = True
        DataGridView1.Rows(0).Cells("
累計結餘").ReadOnly = False

        '
自動調整資料行大小
        Me.DataGridView1.AutoResizeColumns()

    End Sub

    Private Sub CellValueChanged(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles DataGridView1.CellValueChanged

        '
每當有任何儲存格的值變更時就更新累計結餘資料行
        UpdateBalance()
    End Sub

    Private Sub RowsRemoved(ByVal sender As Object, _
        ByVal e As DataGridViewRowsRemovedEventArgs) _
        Handles DataGridView1.RowsRemoved

        '
每當有任何資料列被刪除時就更新累計結餘資料行
        UpdateBalance()
    End Sub

    Private Sub UpdateBalance()
        Dim counter As Integer
        Dim balance As Integer
        Dim deposit As Integer
        Dim withdrawal As Integer

        '
循環處理起始累計結餘資料列以外的每一列
        For counter = 1 To (DataGridView1.Rows.Count - 2)
            deposit = 0
            withdrawal = 0
            balance = Integer.Parse(DataGridView1.Rows(counter - 1) _
                .Cells("
累計結餘").Value.ToString())

            If Not DataGridView1.Rows(counter) _
                .Cells("
存入").Value Is Nothing Then

                '
確認儲存格值不是一個空字串
                If Not DataGridView1.Rows(counter) _
                    .Cells("
存入").Value.ToString().Length = 0 Then
                    deposit = Integer.Parse(DataGridView1.Rows(counter) _
                        .Cells("
存入").Value.ToString())
                End If
            End If

            If Not DataGridView1.Rows(counter) _
                .Cells("
支出").Value Is Nothing Then
                If Not DataGridView1.Rows(counter) _
                    .Cells("
支出").Value.ToString().Length = 0 Then
                    withdrawal = Integer.Parse(DataGridView1.Rows(counter) _
                        .Cells("
支出").Value.ToString())
                End If
            End If

            DataGridView1.Rows(counter).Cells("
累計結餘").Value = _
                (balance + deposit + withdrawal).ToString()
        Next
    End Sub

    Private Sub CellValidating(ByVal sender As Object, _
        ByVal e As DataGridViewCellValidatingEventArgs) _
        Handles DataGridView1.CellValidating

        Dim testInteger As Integer

        If Not e.ColumnIndex = 0 Then
            If Not e.FormattedValue.ToString().Length = 0 Then

                '
嘗試將儲存格值轉換成一個整數
                If Not Integer.TryParse(e.FormattedValue.ToString(), _
                    testInteger) Then

                    DataGridView1.Rows(e.RowIndex).ErrorText = _
                        "
錯誤: 無效的輸入"

                    e.Cancel = True
                End If
            End If
        End If
    End Sub

    Private Sub CellValidated(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles DataGridView1.CellValidated

        DataGridView1.Rows(e.RowIndex).ErrorText = Nothing
    End Sub

    Private Sub UserDeletingRow(ByVal sender As Object, _
        ByVal e As DataGridViewRowCancelEventArgs) _
        Handles DataGridView1.UserDeletingRow

        Dim starting
累計結餘Row As DataGridViewRow = DataGridView1.Rows(0)

        '
檢查是否起始累計結餘資料列內含於選取的資料列中
        If DataGridView1.SelectedRows.Contains(starting累計結餘Row) Then

            If e.Row.Equals(starting
累計結餘Row) Then

                '
不允許使用者去刪除起始資料列
                MessageBox.Show("不可以刪除累計結餘列!")
            End If

            '
如果起始資料列被內含, 則取消刪除
            e.Cancel = True

        End If
    End Sub
End
Class