Public Class frmVerbrauchserfassung
Dim WithEvents btnCreate As New Button With
{.Parent = Me, .Text = "Create Table", .Width = 100, .Left = 10, .Top = 10}
Dim WithEvents btnLoad As New Button With
{.Parent = Me, .Text = "Load Table", .Width = 100, .Left = 110, .Top = 10}
Dim WithEvents btnSave As New Button With
{.Parent = Me, .Text = "Save Table", .Width = 100, .Left = 210, .Top = 10}
Dim WithEvents btnSumme As New Button With
{.Parent = Me, .Text = "Verbrauch", .Width = 100, .Left = 320, .Top = _
10}
Dim dt As DataTable
Dim bs As New BindingSource
Dim WithEvents dgv As New DataGridView With {.Parent = Me, .Top = 50, .Left _
= 10, .DataSource = bs,
.SelectionMode = DataGridViewSelectionMode.FullRowSelect}
Private Sub frmVerbrauchserfassung_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
Me.MinimumSize = New Size(450, 300)
Me.FormBorderStyle = FormBorderStyle.Sizable
End Sub
Private Sub frmVerbrauchserfassung_Resize(sender As Object, e As EventArgs) _
Handles Me.Resize
dgv.Width = Me.ClientRectangle.Width - 20
dgv.Height = Me.ClientRectangle.Height - 100
End Sub
Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles _
btnCreate.Click
bs.DataSource = Nothing
dt = New DataTable
dt.TableName = "Verbrauchserfassung"
With dt.Columns
.Add("Ablesedatum", GetType(Date))
.Add("Strom", GetType(Double))
.Add("Wasser", GetType(Double))
.Add("Gas", GetType(Double))
End With
bs.DataSource = dt
End Sub
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles _
btnLoad.Click
bs.DataSource = Nothing
dt = Nothing
dt = New DataTable
Using ofd As New OpenFileDialog
ofd.Filter = "Verbrauchstabelle XML|*.xml"
ofd.CheckFileExists = True
If ofd.ShowDialog = DialogResult.Cancel Then Exit Sub
dt.ReadXml(ofd.FileName)
bs.DataSource = dt
End Using
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles _
btnSave.Click
If dt Is Nothing Then Exit Sub
Using sfd As New SaveFileDialog
sfd.Filter = "Verbrauchstabelle XML|*.xml"
sfd.CheckPathExists = True
If sfd.ShowDialog = DialogResult.Cancel Then Exit Sub
dt.WriteXml(sfd.FileName, XmlWriteMode.WriteSchema)
End Using
End Sub
Private Sub dgv_DataError(sender As Object, e As _
DataGridViewDataErrorEventArgs) Handles dgv.DataError
MsgBox(e.Exception.Message)
End Sub
Private Sub btnSumme_Click(sender As Object, e As EventArgs) Handles _
btnSumme.Click
If dt Is Nothing Then Exit Sub
Dim wasser1, strom1, gas1, vl As Double, rc As Integer
Dim wasser, strom, gas As Double
Try
For i As Integer = 0 To dgv.Rows.Count - 1
Dim row As DataGridViewRow = dgv.Rows(i)
If row.Selected Then
rc += 1
If strom1 = 0 Then
If Double.TryParse(row.Cells("Strom").Value.ToString, _
vl) Then
strom1 = vl
End If
End If
If wasser1 = 0 Then
If Double.TryParse(row.Cells("Wasser").Value.ToString, _
vl) Then
wasser1 = vl
End If
End If
If gas1 = 0 Then
If Double.TryParse(row.Cells("Gas").Value.ToString, vl) _
Then
gas1 = vl
End If
End If
If Double.TryParse(row.Cells("Wasser").Value.ToString, vl) _
Then
wasser = vl
End If
If Double.TryParse(row.Cells("Gas").Value.ToString, vl) Then
gas = vl
End If
If Double.TryParse(row.Cells("Strom").Value.ToString, vl) _
Then
strom = vl
End If
End If
Next i
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
If rc < 2 Then
MsgBox("Es müssen mindestens zwei Zeilen (Start/Ende der Periode)" & _
"gewählt werden")
Else
MsgBox("Verbrauch gemäß gewählter Zeilen (" & rc.ToString & "):" & _
vbCrLf &
"Strom: " & (strom - strom1).ToString & vbCrLf &
"Gas:" & (gas - gas1).ToString & vbCrLf &
"Wasser: " & (wasser - wasser1).ToString)
End If
End Sub
End Class |