Question DataGridView-When a value in 1 cell changes,how to update another cell with new value

robyn

New member
Joined
May 28, 2008
Messages
1
Programming Experience
Beginner
In my program I have a DataGridView which shows a table of orders. I want when a user changes a number in the quantity column, for my program to update the price column either after the user has left the cell or when they click 'Update Order'.

I could use a sub (as below) to change the price column after the quantity cell value has changed.

VB.NET:
Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles OrderGrid.CellValueChanged

I then need code in this sub to work out the prices.

This is my code I have to work out pricing. At the moment it only works for labels/textboxes. I need for it to be able to work for cells in a DataGridView, so txtQty.Text is a cell from the quantity column. I need this code to loop through each row in the DataGridView.

VB.NET:
product = cDBProduct.GetProduct(lblArtNo.Text)

            If txtQty.Text >= product.Qd5 Then
                lblSubtotal.Text = CStr((CDbl(product.DisPrice5) / 100) * CDbl(txtQty.Text))

            ElseIf txtQty.Text >= product.Qd4 Then
                lblSubtotal.Text = CStr((CDbl(product.DisPrice4) / 100) * CDbl(txtQty.Text))

            ElseIf txtQty.Text >= product.Qd3 Then
                lblSubtotal.Text = CStr((CDbl(product.DisPrice3) / 100) * CDbl(txtQty.Text))

            ElseIf txtQty.Text >= product.Qd2 Then
                lblSubtotal.Text = CStr((CDbl(product.DisPrice2) / 100) * CDbl(txtQty.Text))

            ElseIf txtQty.Text >= product.Qd1 Then
                lblSubtotal.Text = CStr((CDbl(product.DisPrice1) / 100) * CDbl(txtQty.Text))

            ElseIf txtQty.Text < product.Qd1 Then
                lblSubtotal.Text = CStr((CDbl(product.SalPrice) / 100) * CDbl(txtQty.Text))
            End If

My DataGridView is populated by the following code.

VB.NET:
Dim connection As OleDbConnection = DBconnect.GetConnection2
        Dim selectStatement As String = "SELECT cusorders.* FROM cusorders;"
        Dim da As New OleDbDataAdapter(selectStatement, connection)
        Dim CmdB As New OleDbCommandBuilder(da)

        Dim DS As New DataSet

        da.Fill(DS, "Table1")
        OrderGrid.DataSource = DS.Tables("Table1").DefaultView
        OrderGrid.Refresh()
        Cursor.Current = Cursors.Default

Can anyone help me? I'm really stuck.
 
Hello robyn,

you can access each cell of a DataGRidView with Item-Property:
VB.NET:
DataGridView.Item(columnIndex, rowIndex).Value
'or
DataGridView.Item(columnName, rowIndex).Value

Also, you can loop through all lines:
VB.NET:
for each row as DataGridViewRow in DataGridView
      DataGridView.Item([i]column[/i], row.Index).Value = [i]Value[/i]
Next

NOw, you could put your little function into a real function.
VB.NET:
Public Function getPrice(ByVal artNo as String, ByVal quantity as Integer) as ?Double?

Dim product as ??? = cDBProduct.GetProduct(artNo)

If quantity >= product.Qd5 Then
      getPrice = CStr((CDbl(product.DisPrice5) / 100) * CDbl(txtQty.Text))
...
...
...
End Function

Now you could do that:
VB.NET:
for each row as DataGridViewRow in DataGridView
      DataGridView.Item([i]column[/i], row.Index).Value = getPrice(DataGridView.Item([i]column[/i], row.Index).Value, DataGridView.Item([i]column[/i], row.Index).Value)
Next

Hope I helped,
Bobby
 
Back
Top