Results 1 to 2 of 2

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

  1. #1
    gbhs is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2013
    Posts
    11
    Reputation
    53

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

    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

    Code:
    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?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,807
    Reputation
    1705
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •