.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

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.

Advertisements

July 6, 2007 - Posted by | DataGridView, VB.NET

3 Comments »

  1. 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

    Comment by José María Arrabal Alcañiz | October 30, 2007 | Reply

  2. 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

    Comment by w1t0ut2 | May 5, 2008 | Reply

  3. 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.

    Comment by C | June 29, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: