我有一个datagridivew,其中包含许多列,其中包含两个组合框。第一个组合框称为“ cbxCampaign2”,第二个组合框称为“ cbxPositionNo”。当使用表结果进行加载时,我会填充两个框,请参见此处的代码...
'Populate DataGrid3 - Applicant Offers and Recruit
cmd = New SqlCommand("GetApplicantSuccessfulCampaigns", conn)
cmd.Parameters.AddWithValue("@AppID", Me.intAppID)
cmd.CommandType = CommandType.StoredProcedure
da3 = New SqlDataAdapter(cmd)
da3.Fill(ds, "CampignSuccess")
Me.cbxCampaign2.DataSource = ds.Tables("CampignSuccess")
Me.cbxCampaign2.DisplayMember = "CampaignName"
Me.cbxCampaign2.ValueMember = "ID"
' Also changed when Clicked in DGV3
cmd = New SqlCommand("SELECT ID, PositionNo + ', ' + Title As PositionName FROM TblPosition ORDER By PositionNo ASC", conn)
da3 = New SqlDataAdapter(cmd)
da3.Fill(ds, "PositionNames")
Me.cbxPositionNo.DataSource = ds.Tables("PositionNames")
Me.cbxPositionNo.DisplayMember = "PositionName"
Me.cbxPositionNo.ValueMember = "ID"
然后我正在尝试根据第一个组合框的值来过滤第二个组合框的内容。
到目前为止,我有两个事件CellBegin Edit和CellEndEdit并强制转换为新的“ dgvcbPositionNo”,为此添加了新的数据源,但是它不起作用并且不会产生错误!我添加了一些消息框,它们显示正确的结果!我觉得我错过了一些东西,但不确定在哪里!
请参见下面的代码...
Private Sub DataGridView3_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView3.CellBeginEdit
If e.ColumnIndex = 3 Then
MsgBox(Me.DataGridView3.CurrentRow.Cells("cbxCampaign2").Value)
' Set the combobox cell datasource to the filtered BindingSource
Dim dgvcbPostionNo As DataGridViewComboBoxCell = DirectCast(DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
cmd = New SqlCommand("SELECT ID, Title +', ' + PositionNo As PositionName FROM TblPosition WHERE ID IN (SELECT PositionID FROM TblCampaignPositions WHERE CampaignID = @CampID) ORDER By PositionNo", conn)
cmd.Parameters.AddWithValue("@CampID", Me.DataGridView3.CurrentRow.Cells("cbxCampaign2").Value)
da3 = New SqlDataAdapter(cmd)
If Not IsNothing(ds.Tables("PositionFiltered")) Then
ds.Tables("PositionFiltered").Rows.Clear()
End If
da3.Fill(ds, "PositionFiltered")
MsgBox(ds.Tables("PositionFiltered").Rows.Count)
dgvcbPostionNo.DataSource = ds.Tables("PositionFiltered")
dgvcbPostionNo.DisplayMember = "PositionName"
dgvcbPostionNo.ValueMember = "ID"
End If
End Sub
Private Sub DataGridView3_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView3.CellEndEdit
If e.ColumnIndex = 3 Then
MsgBox("A")
' Set the combobox cell datasource to the filtered BindingSource
Dim dgvcbPostionNo As DataGridViewComboBoxCell = DirectCast(DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
dgvcbPostionNo.DataSource = ds.Tables("PositionNames")
End If
End Sub
结束班级