在WinForm具有一个DateTimePicker 4个定期texboxes和2个掩蔽texboxes和绑定datagridview的。
我正在使用datagridview的cellformatting事件从另一个数据表中查找员工的姓名。为了实现这一点,我使用了大部分工作的sql语句。
因为它现在正在从第二个数据表中的不同列中返回雇员姓名,并将其设置在数据网格视图的第二列中,并且在雇员ID之后立即设置。
不工作的部分正在查看终止日期(“FSalida”列)并仅在没有终止日期或选定日期早于终止日期时返回雇员姓名。换句话说,如果雇员已经被终止,它不应该在终止日期之后出现。
是我的代码是这样的:
Private Sub PartePersonalDataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
'This looks up the employee's full name using the employee number from the ID column (first column)
Try
Dim dgvr As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
If dgvr.Cells(0).Value IsNot Nothing AndAlso dgvr.Cells(0).Value IsNot DBNull.Value Then
Dim empID As Integer = CInt(dgvr.Cells(0).Value)
Dim qry = From dr As PersonalObraDataSet.PersonalObRow In PersonalObraDataSet.PersonalOb
Where (dr.cdTrabajador = empID) And (dr.FSalida = Nothing) Or (dr.FSalida <= txtDate.Text)
'This returns each part of the name and joins it in the the 2nd column (name column)
DataGridView1.Rows(e.RowIndex).Cells(1).Value = (qry.First.Nombre1 & " " & qry.First.Nombre2 & " " & qry.First.Apellido1 & " " & qry.First.Apellido2)
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = DefaultBackColor
End If
'If there is an exemption like the employee doesn't exists this turns the background color red and instead
'of the name on the second column it shows "employee doesn't exist."
Catch ex As Exception
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red
DataGridView1.Rows(e.RowIndex).Cells(1).Value = "ESTE EMPLEADO NO EXISTE"
Exit Sub
不工作的部分是后“和”:
Where (dr.cdTrabajador = empID) And (dr.FSalida = Nothing) Or (dr.FSalida <= txtDate.Text)
这是数据的上查找样本表
ProjectID EmployeeID DepartmentID HIreDate TerminationDate Name Middle Last
3116 83 1 09/03/2012 27/04/2012 John Doe Doe2
3116 373 1 16/11/2012 Pedro John Ortiz
3116 1 1 01/01/2013 Jose Maria Applessed
你的帮助解决这个将不胜感激。
我得到的错误DBNull无法转换为'日期',这就是为什么我首先将它改为Nothing的原因。任何其他想法? – David
是啊,检查我的编辑=] – Sean
对不起,但我不知道如何做任何一个。当你说的字段DateTime.MinValue你的意思就像Dim Fsalida DateTime.MinValue?因为你可以说我对编码很陌生。 – David