Link to home
Start Free TrialLog in
Avatar of fizzlefry
fizzlefry

asked on

Add / Update Unbound column in Bound Datagridview

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")

        connection.sqlSBO.Open()

        sAdapter.Fill(sDs, "Qty")

        connection.sqlSBO.Close()

        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)
        Next

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

        UpdateQOH()

    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.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fizzlefry
fizzlefry

ASKER

CodeCruiser...  as always...  thank you for your EXACT fix!!!  I swear, I'll get this yet!!!
Glad to help :-)