我是 VB.NET 的新手。
我有一个带有数据表数据源的 Datagridview。它有两个组合框。我正在使用以下代码填充数据网格视图中的特定组合框,该组合框基于 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 时,我编写了以下代码来填充 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
这里第二个组合框不是基于第一个组合框填充的。任何建议都会有所帮助。