We help IT Professionals succeed at work.
Get Started

Add / Update Unbound column in Bound Datagridview

Last Modified: 2012-07-03
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.
Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE