vb.net - How to decide when cellformatting event is fired? -


i'm using cellformatting use employee id column of bound datagridview "employee id" in datatable , return employee name on unbound "name" column.

  private sub partepersonaldatagridview_cellformatting(byval sender object, byval e system.windows.forms.datagridviewcellformattingeventargs) handles datagridview1.cellformatting     if datagridview1.rowcount > 0 andalso e.rowindex > -1         dim dgvr datagridviewrow = datagridview1.rows(e.rowindex)         dim empid integer = cint(dgvr.cells(0).value)         dim qry = dr personalobradataset.personalobrow in _personalobradataset.personalob _                   dr.cdtrabajador = empid         if qry.count > 0             datagridview1.rows(e.rowindex).cells(1).value = qry.first.nombre1             'datagridview1.rows(e.rowindex).cells(5).value = qry.first.nombre2         end if     end if end sub 

everything load fine , required name each id loaded fine, when the new row added, cellformatting event fires before there chance type in new employee id, giving dbnull error, because cell looking @ empty.

i've looked while , can't find way tell cellformatting fire after cell edit done or on cell leave, or not format cell if field employee id empty.

i don't think can "decide" when cellformating event fired. if understand issue, think need test if cell.value null

this line of code give error if cell.value null.

dim empid integer = cint(dgvr.cells(0).value) 

so need this:

if dgvr.cells(0).value isnot nothing andalso dgvr.cells(0).value isnot dbnull.value    dim empid integer = cint(dgvr.cells(0).value)     (...)  end if 

Comments