简体   繁体   中英

DataGridView Replacing Null Value

I'm trying to replace null values in my datagridview in VB. Data is being read in from an access data base. Every time I run it I get an error relating to "System.NullReferenceException" .

Sub dataGridView1_CellFormatting(ByVal sender As Object, _
  ByVal e As DataGridViewCellFormattingEventArgs) _
  Handles DataGridView1.CellFormatting

    If e.ColumnIndex = Me.DataGridView1.Columns(7).Index Then _
      ' AndAlso (e.Value IsNot Nothing) Then


        With Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)

            If e.Value.Equals("") Then
                e.Value = "very bad"
            End If
        End With

    End If

End Sub

Any help on this issue would be appreciated!!

You need to check the actual database call. You can check if any of the fields are Null and replace them there. Then you do not have to handle them in code...

 IIF(ISNULL(yourcolumn),'', yourcolumn)

This will check if the column is null and if so replace it with empty string, otherwise it will return the value...

Try IsDBNull :

Dim abc As Integer 

abc= IIf(IsDBNull(DGVInvoice.Rows(max).Cells(3).Value) = True, 0, DGVInvoice.Rows(max).Cells(3).Value)

DGVInvoice is your DataGrid and max is your maximum row number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM