如何根据VB.NET中另一个组合框的选择在数据网格视图中填充特殊组合框

时间:2012-07-11 08:15:55

标签: vb.net

我是VB.NET的新手。

我有一个Datagridview与Datatable的数据源。它有两个Combobox。我正在使用以下代码在数据网格视图中填充特定的组合框,基于VB.NET中另一个组合框的选择。

Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Dim cmb As ComboBox = TryCast(e.Control, ComboBox)
    If DataGridView1.CurrentCell.ColumnIndex = 1 Then
        If (cmb IsNot Nothing) Then
            RemoveHandler cmb.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
            AddHandler cmb.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
        End If
    End If
End Sub

Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    If DataGridView1.CurrentCell.ColumnIndex = 1 Then
        Dim comboBox As ComboBox = CType(sender, ComboBox)
        Dim cbCell As DataGridViewComboBoxCell = DirectCast(DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(2), DataGridViewComboBoxCell)
        s = comboBox.Text
        If String.Compare(s, "Driver") = 0 Then
            cbCell.Items.Clear()
            con.Open()
            cmd = New SqlCommand("Select EmpName from EmployeeDetails where Status='Active' and Designation='Driver'", con)
            dr = cmd.ExecuteReader()
            While (dr.Read())
                cbCell.Items.Add(dr("EmpName"))
            End While
            dr.Close()
            cmd.Dispose()
            con.Close()
        End If
    End If
 End Sub

它工作正常,但当我关闭表单和repoen然后,我被wriiten以下代码填充datagridview

Public Function view()
    con.Open()
    cmd = New SqlCommand("select SalaryDate,Type,EmpName,StDate,EnDate,Months,Days,ActualSalary,ReceivedSalary,Balance from LeaseDriverSalary where LeaseNo=" + sele.ToString, con)
    da = New SqlDataAdapter(cmd)
    dt = New DataTable()
    da.Fill(dt)
    Dim row As DataSet1.LeaseDriverSalary1Row
    j = 1
    For i = 0 To dt.Rows.Count - 1 Step 1
        row = DataSet1.LeaseDriverSalary1.NewRow
        DataSet1.LeaseDriverSalary1.Rows.Add(row)
        row.SalaryDate = dt.Rows(i)(0)
        row.Type = dt.Rows(i)(1).ToString
        row.EmpName = dt.Rows(i)(2).ToString
        row.StDate = dt.Rows(i)(3).ToString
        row.EnDate = dt.Rows(i)(4).ToString
        row.Months = Convert.ToInt64(dt.Rows(i)(5).ToString())
        row.Days = Convert.ToInt64(dt.Rows(i)(6).ToString())
        row.ActualSalary = Convert.ToInt64(dt.Rows(i)(7).ToString())
        row.ReceivedSalary = Convert.ToInt64(dt.Rows(i)(8).ToString())
        row.Balance = Convert.ToInt64(dt.Rows(i)(9).ToString())
    Next
    j = 0
    cmd.Dispose()
    con.Close()
    Return 0
 End Function

这里第二个组合框没有基于第一个组合框填充。 任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:0)

重新打开窗口时,会在第一个ComboBox上导致SelectedIndexChanged?将SelectedIndexChanged的代码放在函数“UpdateSecondComboBox”中,然后在SelectedIndexChanged处理函数和View()函数中调用它。

相关问题