troubleshooting Question

Add / Update Unbound column in Bound Datagridview

Avatar of fizzlefry
fizzlefry asked on
Visual Basic.NET.NET ProgrammingProgramming
3 Comments1 Solution2231 ViewsLast Modified:
I have scoured and cannot understand this issue.  I have a datagridview that I'm binding on form load as follows:

Dim sql As String = "SELECT PartNo, PartName, UnitQOH, QtyOH FROM tblInvQty ORDER BY PartName ASC"
        Dim sCommand As New SqlCommand(sql, connection.sqlSBO)
        Dim sAdapter As New SqlDataAdapter(sCommand)
        Dim sBuilder As New SqlCommandBuilder(sAdapter)
        Dim sDs As New DataSet()
        Dim sTable As DataTable = sDs.Tables("Qty")


        sAdapter.Fill(sDs, "Qty")


        DataGridView1.DataSource = sDs.Tables("Qty")
        DataGridView1.ReadOnly = False
        DataGridView1.Columns(0).ReadOnly = True
        DataGridView1.Columns(1).ReadOnly = True
        DataGridView1.Columns(2).ReadOnly = True
        DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
        DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
        DataGridView1.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
        DataGridView1.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
        DataGridView1.Columns(2).HeaderText = "Current QOH"

I then want to add an unbound column, just as a reference.  I'm using the following immediately after the above listed code:

DataGridView1.Columns.Add("NewQOH", "New QOH")
        DataGridView1.Columns(4).ReadOnly = True
        DataGridView1.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells

This column is used as a reference so as the invoice is entered, the new QOH should adjust, so they can see the increase in inventory.  I am adjusting the value on load, as such:

Dim i As Integer
        For i = 0 To Me.DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells("NewQOH").Value = (DataGridView1.Rows(i).Cells("QtyOH").Value + DataGridView1.Rows(i).Cells("UnitQOH").Value)

This ALL works fine.  Until I want to update the New QOH with the amount entered in the QtyOH cell.  I have searched and searched and it seems the following code, from what I'm seeing, should do it:

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


    End Sub

where UpdateQOH() is:

Private Sub UpdateQOH()

        Dim rowNum As Integer
        rowNum = DataGridView1.CurrentRow.Index

        DataGridView1.Item(4, rowNum).Value = (DataGridView1.Item(3, rowNum).Value + DataGridView1.Item(2, rowNum).Value)

    End Sub

If I use UpdateQOH in the CellValueChanged event, my new column disappears from the grid completely.  However, if I just comment out the function, so it doesn't run, the column shows fine, and the math is correct.  I can't for the life of me, figure out how to update the corresponding cell, when a new QOH is entered.  My column keeps disappearing.  I was going to just add the entered amount to "UnitQOH", however I was hoping to show them a before and after:  here was your OH, here is your entry, this should be the new OH.
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros