Datagridview grid references change after showing a new form - help please!

DoJa

Member
Joined
Jul 26, 2011
Messages
21
Programming Experience
Beginner
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:

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:
Ok so i've started again from scratch, basically copied the example from here How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List and modified to suit my requirements.

I have a form with a datagridview control and a button. I have disabled autogeneration of columns from bound datasources and manually created all my datagridview columns in code.

I also have a datasource, a manually created datatable/dataset which i have linked to my datatable as per the msdn example.

Finally i've added a button to populate the datasource with data.


Loading the form puts all my manually coded columns into the datagridview and i'm not getting any duplicated fields.So far so good. However when I try to add data to the datasource, a new line is generated in the datagridview but it contains no data!

Where have I gone wrong?!

Public Class Form1
    Dim table As New DataTable
    Dim dataset As New DataSet
    Dim row As DataRow ' Register Datarows

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        createDatasource()
        dgvRegister.AutoGenerateColumns = False
        dgvRegister.DataSource = table
        createDatagridview()
    End Sub

    ' Add Columns to DataGridView control.
    Private Sub createDatagridview()

        ' Define ID Column
        Dim colCustomerid As New DataGridViewTextBoxColumn()
        colCustomerid.Name = "id"
        colCustomerid.DataPropertyName = "colCustomerid"
        colCustomerid.ReadOnly = True

        ' Define Name Column
        Dim colFullname As New DataGridViewTextBoxColumn()
        colFullname.Name = "Name"
        colFullname.DataPropertyName = "colFullname"
        colFullname.ReadOnly = True

        ' Define 'Remove' Button
        Dim colX As New DataGridViewButtonColumn()
        colX.HeaderText = "X"
        colX.Name = "X"
        colX.Text = "X"
        colX.UseColumnTextForButtonValue = True

        ' Define From Cash Column
        Dim colFromcash As New DataGridViewTextBoxColumn()
        colFromcash.Name = "From Cash"
        colFromcash.DataPropertyName = "colFromcash"
        colFromcash.ReadOnly = True

        ' Define From Account Column
        Dim colFromaccount As New DataGridViewTextBoxColumn()
        colFromaccount.Name = "From Account"
        colFromaccount.DataPropertyName = "colFromaccount"
        colFromaccount.ReadOnly = True

        'Define Update Value Column
        Dim colUpdateValue As New DataGridViewTextBoxColumn()
        colUpdateValue.Name = "Update Value"
        colUpdateValue.DataPropertyName = "colUpdateValue"
        colUpdateValue.ReadOnly = True

        'Add columns to datagridview control
        dgvRegister.Columns.Add(colCustomerid)
        dgvRegister.Columns.Add(colFullname)
        dgvRegister.Columns.Add(colX)
        dgvRegister.Columns.Add(colFromcash)
        dgvRegister.Columns.Add(colFromaccount)
        dgvRegister.Columns.Add(colUpdateValue)

    End Sub

    Private Sub createDatasource()

            ' Create New Data Table
            table = New DataTable()


            'Declare Column Variables
            Dim colCustomerid As DataColumn
            Dim colFullname As DataColumn
            Dim colFromcash As DataColumn
            Dim colFromaccount As DataColumn
            Dim colUpdateValue As DataColumn

            ' Create Empty Columns
            colCustomerid = New DataColumn("ID", Type.GetType("System.String"))
            colFullname = New DataColumn("Name", Type.GetType("System.String"))
            colFromcash = New DataColumn("Cash", Type.GetType("System.String"))
            colFromaccount = New DataColumn("Account", Type.GetType("System.String"))
            colUpdateValue = New DataColumn("Update Value", Type.GetType("System.String"))

            ' Add Coulumns to table
            table.Columns.Add(colCustomerid)
            table.Columns.Add(colFullname)
            table.Columns.Add(colFromcash)
            table.Columns.Add(colFromaccount)
            table.Columns.Add(colUpdateValue)

            'Add datatable to dataset
        dataset.Tables.Add(table)


    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        row = table.NewRow

        row("Cash") = "1"
        row("Update Value") = "0"
        row("ID") = "345"
        row("Name") = "dave"

        table.Rows.Add(row)
     
    End Sub
End Class

 
Come on man, please be a bit more specific. I'm tearing my hair out here! As far as I can see everything is there .. I am confused!
 
.DataPropertyName = "colFromaccount"
New DataColumn("Account",
"Account" <> "colFromaccount"
I'm tearing my hair out here!
Using typed datasets should save you lots of tearing. Visual comes before Basic in Visual Basic.
 
Back
Top