Datagridview- set cell value to null based on another cell's value

gbhs

Member
Joined
Jan 19, 2013
Messages
11
Programming Experience
Beginner
Hi to forum members
I have a problem in a datagridview .
Col2 must be null if col1 is null.
I have used a cellvaluechanged event with

VB.NET:
if isdbnull(mydgv.rows(i).cells("Col1").value) orelse mydgv.rows(i).cells("Col1").value.tostring.equals(dbnull.value) orelse _
string.isnullorwhitespace(mydgv.rows(i).cells("Col1").value) then
isdbnull(mydgv.rows(i).cells("Col2").value)
end if

to no success.
How should I do it?
 
If you're not using e.RowIndex and e.ColumnIndex in that event handler then you're doing it wrong. They are what tell you what row and column the cell value changed in. Isn't that important information?
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    If e.ColumnIndex = sourceColumnIndex Then
        Dim row = DataGridView1.Rows(e.RowIndex)

        If row.Cells(sourceColumnIndex).Value Is DBNull.Value Then
            row.Cells(destinationColumnIndex).Value = DBNull.Value
        End If
    End If
End Sub
 
Back
Top