Hi All,
I'm using a datagridview to display a list of names added to a register (in a datatable) which then later get copied to an external database. The system worked perfectly until recently when I added a second form to my project. I noticed if I changed forms from the one with the dgv to another and then back again, I lose the ability to delete items from my dgv using the buttons in the 'delete button' column.
A bit of investigation revealed that the reason for this was because when changing between forms, the grid reference for the dgv changed. To begin with the column with my 'x' buttons was '5' but after flicking between forms this changed to '0' , the other column reference values also changed. There is nothing in the onload/onshow or on leave/hide events for either form which could cause this and I'm at a total loss as to why this is happening. Anyone able to point me in the right direction or give some advice?
Here's a copy of the code i've got in the onclick event for the datagridview:
I'm using a datagridview to display a list of names added to a register (in a datatable) which then later get copied to an external database. The system worked perfectly until recently when I added a second form to my project. I noticed if I changed forms from the one with the dgv to another and then back again, I lose the ability to delete items from my dgv using the buttons in the 'delete button' column.
A bit of investigation revealed that the reason for this was because when changing between forms, the grid reference for the dgv changed. To begin with the column with my 'x' buttons was '5' but after flicking between forms this changed to '0' , the other column reference values also changed. There is nothing in the onload/onshow or on leave/hide events for either form which could cause this and I'm at a total loss as to why this is happening. Anyone able to point me in the right direction or give some advice?
Here's a copy of the code i've got in the onclick event for the datagridview:
Private Sub dgvRegister_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvRegister.CellContentClick MessageBox.Show(varRowcount) 'Prevent every name from being deleted from the register list (If all names are removed, new names cannot be added) If varRowcount > 0 Then MessageBox.Show(e.ColumnIndex) 'Ensure that rows are only deleted when the X button is pressed (column index 5) If e.ColumnIndex = 5 Then 'Confirm deletion of member from register Dim msg As DialogResult = MessageBox.Show("Are you sure you want to remove " & dgvRegister.Item(1, dgvRegister.CurrentRow.Index).Value & "from today's register?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If msg = vbNo Then Exit Sub End If 'Remove Selected Row dgvRegister.Rows.Remove(dgvRegister.CurrentRow) 'Update row count varRowcount = varRowcount - 1 lblTotalval.Text = varRowcount End If Else lblStatus.Text = "You cannot delete another name from the register list until you add another member" End If End Sub
Last edited by a moderator: