1

私はVB.NETが初めてです。

私は Datatable のデータソースを持つ Datagridview を持っています。2 つのコンボボックスがあります。VB.NET での別のコンボボックスの選択に基づいて、datagridview で特定のコンボボックスを埋めるために次のコードを使用しています。

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

正常に動作していますが、フォームを閉じてレポーンしたときに、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

ここで、2 番目のコンボボックスは、最初のコンボボックスに基づいて塗りつぶされませんでした。どんな提案も役に立ちます。

4

1 に答える 1

0

ウィンドウを再度開くと、最初のComboBoxでSelectedIndexChangedが発生する原因は何ですか?SelectedIndexChangedのコードを関数'UpdateSecondComboBox'に配置し、SelectedIndexChangedハンドラーとView()関数の両方で呼び出します。

于 2012-07-11T11:31:01.217 に答える