How to put conditions on a cellformatting event?

  • Thread starter Thread starter david12king
  • Start date Start date
D

david12king

Guest
Hello,

Im using a cellformatting even to use the Employee ID Column of a bound datagridview to look up the "Employee ID" in another datatable and return the Employee name on the UNbound "Name" Column.

Private Sub PartePersonalDataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If DataGridView1.RowCount > 0 AndAlso e.RowIndex > -1 Then
Dim dgvr As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
Dim empID As Integer = CInt(dgvr.Cells(0).Value)
Dim qry = From dr As PersonalObraDataSet.PersonalObRow In _PersonalObraDataSet.PersonalOb _
Where dr.cdTrabajador = empID
If qry.Count > 0 Then
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 and the required name for each ID are loaded fine, but when the the new row is added, the cellformatting event fires before there is a chance to type in the New Employee ID, giving a DBNull error, because the cell it is looking at is empty.

Ive looked for a while and i cant find a way to tell the cellformatting to fire after the cell edit is done or on cell leave, or to not format the cell if the field Employee ID is empty.

Thank You.

Continue reading...
 
Back
Top