添加新记录时的默认值

时间:2019-05-15 20:13:30

标签: vb.net winforms

我尝试使用“绑定导航器添加”按钮添加新记录。我有三个字段可以在新记录中默认设置。用户ID,开始时间和结束时间。使用下面的代码,我可以设置开始时间和结束时间,但不能设置用户组合框。无法设置该值吗?

我尝试在BindingNavigatorAddNewItem_Click中设置值

Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
    'MsgBox("Add new Log")
    Dim User As String = currUser.Name
    Dim Index As Integer = cmbOperator.FindString(User)
    cmbOperator.SelectedIndex = Index
    cmbOperator.SelectedItem = User

    dtpLogIn_Date.Value = currUser.Start_Time
    dtpLogOut_Date.Value = currUser.End_Time

End Sub

Form after add new redraw

1 个答案:

答案 0 :(得分:0)

一种解决方案是在BindingSource_PositionChanged事件中设置cmbOperator索引。我的猜测是在记录位置更改后会触发这种情况。然后,将其包装在代码中,以检查是否为新增内容。可能也可以将它放在BindingNavigator_PositionChanged中。

    Dim varShiftKey = Shift_LogBindingSource.Current("Shift_Key")
    If varShiftKey = -1 Then
        Dim User As String = currUser.Name
        Dim Index As Integer = cmbOperator.FindString(User)
        cmbOperator.SelectedIndex = Index
    End If