I was creating a program that will add the value of the first 2 columns in datagridview when you typed it in then the answer will be displayed in the third column... I want to auto update the third column that if I delete the value of one of first two columns the third column will be affected.

Is it possible to update the value of a cell if it already has a value?

Dani AI

Generated

Nice catch from — a computed column is the cleanest solution when your grid is bound to a DataTable. For cases where the grid is unbound or you want custom behavior (different parsing rules, rounding, display of blanks, or immediate updates for checkboxes), use the grid events to recalculate the third cell when either of the first two cells change.

Example pattern (VB.NET): keep a small recursion guard, commit edits when the current cell is dirty (useful for checkboxes), parse values safely with TryParse, and write the result back to the third cell.

Private updating As Boolean = False

Private Sub dgv_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged
    If updating OrElse e.RowIndex < 0 Then Return
    If e.ColumnIndex = 0 OrElse e.ColumnIndex = 1 Then
        updating = True
        Dim row = dgv.Rows(e.RowIndex)
        Dim v1, v2 As Integer
        Dim ok1 = Integer.TryParse(Convert.ToString(row.Cells(0).Value), v1)
        Dim ok2 = Integer.TryParse(Convert.ToString(row.Cells(1).Value), v2)
        If ok1 AndAlso ok2 Then
            row.Cells(2).Value = v1 + v2
        Else
            row.Cells(2).Value = Nothing   ' or DBNull.Value for bound tables
        End If
        updating = False
    End If
End Sub

Private Sub dgv_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged
    If dgv.IsCurrentCellDirty Then dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub

Troubleshooting notes: when a cell is cleared the parser will fail — decide whether you want the sum cell blank or zero and write DBNull for DataTable-bound scenarios. Subscribe to DataGridView.DataError to catch parse/display exceptions. If you update the third cell programmatically, guard against recursion (the updating flag above). For very large datasets, prefer server-side calculation or a computed DataColumn for performance.

References: DataGridView.CellValueChanged (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.cellvaluechanged) and CurrentCellDirtyStateChanged (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.currentcelldirtystatechanged).

Recommended Answers

All 3 Replies

>I want to auto update the third column that if I delete the value of one of first two columns the third column will be affected.

Expression column.

Dim dt As New Data.DataTable
        dt.Columns.Add("No1", GetType(Int32))
        dt.Columns.Add("No2", GetType(Int32))
        dt.Columns.Add("Sum", GetType(Int32), "No1+No2")

        datagridView1.DataSource = dt

Thanks, it helps a lot ^_^

You're welcome.

Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.