Can you see why AddRow is adding 2 rows? Imports System.IO Public Class frmMain '…
- User
Can you see why AddRow is adding 2 rows?
Imports System.IO
Public Class frmMain
' Declare the context menu and the item Private contextMenu As ContextMenuStrip Private WithEvents addMenuItem, deleteMenuItem, moveUpMenuItem, moveDownMenuItem As ToolStripMenuItem Private clickedRowIndex As Integer ' Form constructor Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. ' Initialize and add the context menu InitializeContextMenu() ' Initialize the DataGridView InitializeDataGridView() ' Set DataGridView Anchor DataGridView1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Bottom ' Load data when the form loads LoadData() End Sub ' Handle the DataGridView Mouse Down event to get the row index on right click Private Sub DataGridView1_MouseDown(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseDown If e.Button = MouseButtons.Right Then Dim hitTestInfo As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y) If hitTestInfo.Type = DataGridViewHitTestType.Cell Then clickedRowIndex = hitTestInfo.RowIndex DataGridView1.ClearSelection() DataGridView1.Rows(clickedRowIndex).Selected = True End If End If End Sub ' Initialize the context menu only once Private Sub InitializeContextMenu() contextMenu = New ContextMenuStrip() ' Add menu items addMenuItem = New ToolStripMenuItem("Add Row") deleteMenuItem = New ToolStripMenuItem("Delete Row") moveUpMenuItem = New ToolStripMenuItem("Move Up") moveDownMenuItem = New ToolStripMenuItem("Move Down") ' Add items to context menu contextMenu.Items.AddRange(New ToolStripMenuItem() {addMenuItem, deleteMenuItem, moveUpMenuItem, moveDownMenuItem}) ' Assign context menu to DataGridView DataGridView1.ContextMenuStrip = contextMenu ' Attach event handlers AddHandler addMenuItem.Click, AddressOf addMenuItem_Click AddHandler deleteMenuItem.Click, AddressOf deleteMenuItem_Click AddHandler moveUpMenuItem.Click, AddressOf moveUpMenuItem_Click AddHandler moveDownMenuItem.Click, AddressOf moveDownMenuItem_Click ' Initially disable move up and move down menu items moveUpMenuItem.Enabled = False moveDownMenuItem.Enabled = False End Sub ' Event handler for the context menu item click to add a row Private Sub addMenuItem_Click(sender As Object, e As EventArgs) Handles addMenuItem.Click AddRow() UpdateMoveMenuItems() ' Ensure move menu items are updated End Sub ' Event handler for deleting a row Private Sub deleteMenuItem_Click(sender As Object, e As EventArgs) If clickedRowIndex >= 0 AndAlso Not DataGridView1.Rows(clickedRowIndex).IsNewRow Then DataGridView1.Rows.RemoveAt(clickedRowIndex) UpdateMoveMenuItems() End If End Sub ' Event handler for moving a row up Private Sub moveUpMenuItem_Click(sender As Object, e As EventArgs) If clickedRowIndex > 0 AndAlso Not DataGridView1.Rows(clickedRowIndex).IsNewRow Then Dim selectedRow As DataGridViewRow = DataGridView1.Rows(clickedRowIndex) DataGridView1.Rows.Remove(selectedRow) DataGridView1.Rows.Insert(clickedRowIndex - 1, selectedRow) DataGridView1.ClearSelection() DataGridView1.Rows(clickedRowIndex - 1).Selected = True clickedRowIndex -= 1 UpdateMoveMenuItems() End If End Sub ' Event handler for moving a row down Private Sub moveDownMenuItem_Click(sender As Object, e As EventArgs) If clickedRowIndex < DataGridView1.Rows.Count - 2 AndAlso Not DataGridView1.Rows(clickedRowIndex).IsNewRow Then Dim selectedRow As DataGridViewRow = DataGridView1.Rows(clickedRowIndex) DataGridView1.Rows.Remove(selectedRow) DataGridView1.Rows.Insert(clickedRowIndex + 1, selectedRow) DataGridView1.ClearSelection() DataGridView1.Rows(clickedRowIndex + 1).Selected = True clickedRowIndex += 1 UpdateMoveMenuItems() End If End Sub ' Method to update the status of move menu items Private Sub UpdateMoveMenuItems() Dim currentIndex As Integer = DataGridView1.CurrentCell.RowIndex moveUpMenuItem.Enabled = (currentIndex > 0) moveDownMenuItem.Enabled = (currentIndex < DataGridView1.Rows.Count - 2) SaveData() End Sub ' Event handler for DataGridView CellClick to update move menu items Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick If e.RowIndex >= 0 Then UpdateMoveMenuItems() End If End Sub ' Method to initialize DataGridView columns Private Sub InitializeDataGridView() ' Add columns to the DataGridView DataGridView1.Columns.Add("nameColumn", "Name") DataGridView1.Columns.Add("typeColumn", "Type") DataGridView1.Columns.Add("dateColumn", "Date") DataGridView1.Columns.Add("startTimeColumn", "Start Time") DataGridView1.Columns.Add("finishTimeColumn", "Finish Time") DataGridView1.Columns.Add("totalTimeColumn", "Total Time") DataGridView1.Columns.Add("amountColumn", "Amount") DataGridView1.Columns.Add("commentColumn", "Comment") ' Set AutoSizeColumnsMode to Fill DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill ' Set the FillWeight to control the width of each column DataGridView1.Columns("nameColumn").FillWeight = 10 ' Adjust as needed DataGridView1.Columns("typeColumn").FillWeight = 10 ' Adjust as needed DataGridView1.Columns("dateColumn").FillWeight = 10 ' Adjust as needed DataGridView1.Columns("startTimeColumn").FillWeight = 10 ' Adjust as needed DataGridView1.Columns("finishTimeColumn").FillWeight = 10 ' Adjust as needed DataGridView1.Columns("totalTimeColumn").FillWeight = 10 ' Adjust as needed DataGridView1.Columns("amountColumn").FillWeight = 10 ' Adjust as needed DataGridView1.Columns("commentColumn").FillWeight = 30 ' Comment section should be the longest ' Make Total Time column read-only DataGridView1.Columns("totalTimeColumn").ReadOnly = True ' Handle the CellValueChanged event AddHandler DataGridView1.CellValueChanged, AddressOf dataGridView1_CellValueChanged ' Handle the SelectionChanged event to enable/disable Move Up and Move Down ' AddHandler DataGridView1.SelectionChanged, AddressOf DataGridView1_SelectionChanged End Sub ' Method to add a row to the DataGridView Private Sub AddRow() ' Example data to add Dim name As String = " " Dim type As String = " " Dim dateValue As DateTime = DateTime.Parse("12/7") Dim startTime As DateTime = DateTime.Parse("00:00 PM") Dim finishTime As DateTime = DateTime.Parse("1:00 PM") Dim totalTime As TimeSpan = finishTime - startTime Dim amount As Decimal = 0 Dim comment As String = " " ' Create a row with the data Dim row As String() = New String() {name, type, dateValue.ToString("d"), startTime.ToString("HH:mm"), finishTime.ToString("HH:mm"), totalTime.ToString("h\:mm"), "£" & amount.ToString(), comment} ' Add the row to the DataGridView DataGridView1.Rows.Add(row) End Sub ' Method to save data Private Sub SaveData() ' Save data to a CSV file Using writer As New StreamWriter("data.csv") ' Write header writer.WriteLine("Name,Type,Date,Start Time,Finish Time,Total Time,Amount,Comment") ' Write rows For Each row As DataGridViewRow In DataGridView1.Rows ' Skip the new row placeholder If Not row.IsNewRow Then Dim name = If(row.Cells("nameColumn").Value, "").ToString() Dim type = If(row.Cells("typeColumn").Value, "").ToString() Dim dateValue = If(row.Cells("dateColumn").Value, "").ToString() Dim startTime = If(row.Cells("startTimeColumn").Value, "").ToString() Dim finishTime = If(row.Cells("finishTimeColumn").Value, "").ToString() Dim totalTime = If(row.Cells("totalTimeColumn").Value, "").ToString() Dim amount = If(row.Cells("amountColumn").Value, "").ToString() Dim comment = If(row.Cells("commentColumn").Value, "").ToString() writer.WriteLine($"{name},{type},{dateValue},{startTime},{finishTime},{totalTime},{amount},{comment}") End If Next End Using End Sub ' Method to load data Private Sub LoadData() ' Check if the file exists If File.Exists("data.csv") Then Using reader As New StreamReader("data.csv") ' Clear existing rows DataGridView1.Rows.Clear() ' Read header line Dim header As String = reader.ReadLine() ' Read rows While Not reader.EndOfStream Dim line As String = reader.ReadLine() Dim values As String() = line.Split(","c) DataGridView1.Rows.Add(values) End While End Using End If End Sub ' Event handler for cell value changes Private Sub dataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) SaveData() End Sub ' Event handler for form load Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load LoadData() End Sub - Assistant