1

更新

winform 有一个 datetimepicker 4 个常规 texboxes 和 2 个掩码 texboxes 和一个绑定的 datagridview。

我正在使用 datagridview 的 cellformatting 事件从另一个数据表中查找员工的姓名。为了实现这一点,我使用了一个大部分工作的 sql 语句。

就像现在一样,它从第二个数据表的不同列返回员工姓名,并按预期将它们设置在 datagridview 的第二列中,紧跟在员工 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

非常感谢您帮助解决此问题。

4

1 回答 1

0

你已经得到了上面几行的解决方案,使用DBNull.Value而不是Nothing.

Where (dr.cdTrabajador = empID) And (dr.FSalida = DBNull.Value) Or (dr.FSalida <= txtDate.Text)

或者,您可以在构造函数中将FSalida字段设置为类似DateTime.MinValue(我相信这是 DateTime 的默认值)PersonalObRow并检查它,例如:

Class PersonalObRow

Public FSalida As DateTime 'the VB Date keyword is synonymous to System.DateTime

    Public Sub New()

        FSalida = DateTime.MinValue

    End Sub

End Class

或者您可以 make it Nullable(Of DateTime),这将允许您检查它是否是Nothing,例如:

Class PersonalObRow

    Public FSalida As Nullable(Of DateTime) 'or Nullable(Of Date)

End Class
于 2013-07-30T13:41:39.017 回答