Update of DataGridView columns when ComboBox clicked

SteveInBeloit

Well-known member
Joined
May 22, 2006
Messages
132
Programming Experience
10+
Hi,

I have a DataGridView that has a combo box on it. When the combo box is pulled down and a value selected, regardless if it is the same value that was originally in there, I need to change the value of two other columns on that row to nulls.

Then when I do the .Update, the of course need presisted to the underlying Database.

I am having some problems finding the correct place to do this.

This is what I have tried to do:

On the MyDataDridView_cellClick I put

VB.NET:
  If e.ColumnIndex = 0 Then
            DataGridView3.Rows(e.RowIndex).Cells("tkRejectedTime").Value = System.DBNull.Value
            DataGridView3.Rows(e.RowIndex).Cells("tkRejectedBy").Value = System.DBNull.Value
        End If

I put a break point there and watched the code execute, it changes the value, and I can see that on the form. But when I do the .Update, it never makes it back to the dataTable first. What should I do here? Is there a property I should set on the cell after I change the value to indicate it needs to be written back to the dataTable?

Then I thought maybe make the change directly in the DataTable. But
Me.DataTable. lets me pick rows OR columns, don't see how I can narrow it down to the individual cell I want to update.

Thanks,
Steve
 
Here is the next thing I tried, seems to work good.

VB.NET:
   Private Sub DataGridView3_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        If e.ColumnIndex = 0 Then
            Dim dv As DataRowView
            dv = CType(Me.BindingSource3.Current, DataRowView)
            dv("tkRejectedTime") = System.DBNull.Value
            dv("tkRejectedBy") = System.DBNull.Value
        End If
    End Sub
 
Last edited:
Better:

VB.NET:
   Private Sub DataGridView3_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        If e.ColumnIndex = 0 Then
            Dim dt As [B]MyTypedDataRow[/B]
            dt = CType(CType(Me.BindingSource3.Current, DataRowView).Row, [B]MyTypedDataRow[/B])
            dt.tkRejectedTime = System.DBNull.Value
            dt.tkRejectedBy = System.DBNull.Value
        End If
    End Sub

Note, you have to edit the bits in bold to be specific to you
 
Back
Top