Formulas in DataGridView?
I don’t know if this is possible, and I know there are programmatic workarounds, but I was wondering if there was some sort of built-in feature in DataGridViews that I’m missing where you can create formulas for columns, kind of like Excel. If you still don’t follow, if in column1 I have quantity and in column2 I have price, I want column3 to calculate quantity * price dynamically without having to rebind the DataGridView.
Status: Unsolved.
Solution: None yet.
RSS Feed
I have made this solution
1 – Add a new Column to the datagridview
2 – Modify DataGridView Event CellPainting
Private Sub PintaCelda(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Order_DetailsDataGridView.CellPainting
If e.RowIndex -1 Then
If e.ColumnIndex = 2 Or e.ColumnIndex = 3 Then
With Me.Order_DetailsDataGridView.Rows(e.RowIndex)
‘ Column 5 = Total calculated
‘ Column 3 = Qty
‘ Colimn 2 = Price
Try
.Cells(5).Value = .Cells(3).Value * .Cells(2).Value
Catch ex As Exception
End Try
End With
End If
I have tried in my ten months project, and it works, below is my code similar to this post, don’t know if you had receive any comment in this, since it is last year:
With Me.DataGridView2.Rows(DataGridView2.CurrentCell.RowIndex)
.Cells(4).Value -= i
End With
I would say you could catch CellChanged events and update the value of the target cell (within the formula column) only for the current row.
Or, directly update the datasource – a dotNet app will update the datagridVIEW on any change to its dataSOURCE.