如何将数据表作为附加参数传递给事件处理程序

时间:2015-12-16 22:26:24

标签: vb.net winforms datatable

在我的表格上有一个数据代码组合,有两个日期时间选择器。 我试图将数据表传递给我的cbo_SelectedIndexChanged()处理程序,因此当用户选择组合框中的第二个值时,日期时间选择器将使其文本与数据表绑定。

我想知道如何做到这一点,以及最干净的方法是什么。

Private Sub cboTrainType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboTrainType.SelectedIndexChanged
    Dim dt As New DataTable()
    Using sqlConn As New SqlConnection("Data Source=VERITON-005\HR_SQL;Initial Catalog=HR235;User ID=sa;Password=HRSQL@235")
        sqlConn.Open()
        Using cmd As New SqlCommand("sp_SelectAllTraining")
            cmd.CommandType = CommandType.StoredProcedure
            Using da As New SqlDataAdapter(cmd)
                da.SelectCommand.Connection = sqlConn

                da.Fill(dt)
                sqlConn.Close()
                cboEditEmpDept.DisplayMember = "Type"
                cboEditEmpDept.ValueMember = "TrainingID"
                cboEditEmpDept.DataSource = dt
                cboEditEmpDept.Text = ""
            End Using
            sqlConn.Close()
        End Using
    End Using

    Dim selectedindex = cboTrainType.SelectedIndex
    dtpTrainDate.Text = dt.Rows(selectedindex)("trainingDate").ToString
End Sub

0 个答案:

没有答案
相关问题