在selectvaluechange事件触发后,ComboBox会丢失值

时间:2017-05-26 11:47:09

标签: vb.net ms-access data-binding combobox datasource

好的,所以我对此进行了搜索,最后一篇文章的日期是8年前,与我的情况不同。

我有一个vb.net应用程序 - 窗体打开,有3个组合框。每个都是绑定到不同表的数据。第一个过滤第二个,过滤第三个。 在一起,所有3个都充当加载DataSet的过滤器。

这些是rv物料清单的工程图表。我正在选择型号年份,产品系列,然后选择具体型号。我使用ComboBoxes来集体获取我的模型索引 - 然后我根据该模型索引加载DataSet

但是,当我加载DataSource时,模型ComboBox,有时模型年份变为空白,其值无效。

我可以通过Debug.Print确认它的值,我将它赋给一个类属性以用作一种全局变量。

这是我的代码 - 希望有人可以看到我不知道他们为何会被删除的内容。

这是访问数据库的前端。您将看到我现在有一个按钮来执行加载,我曾经触发了最后一个组合索引更改事件,但我得到了相同的结果。

 'define selected chart      
 Dim schart As New Chart
 Private Sub frmchartentry_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'Read the model year table and look for default value.
    'Then use that value to filter the series combobox.

    Call loaddata()
    Call getyear()

    'cmbSeries.Text = ""
    'cmbModel.Text = ""
    TabControl1.SelectedIndex = 0
    schart.Category = TabControl1.SelectedTab.Text
    Debug.Print(schart.Category)

End Sub
Private Sub loaddata()

    Me.TblModelYearTableAdapter.Fill(Me.DsModelYear.tblModelYear)
    Me.TblProductListTableAdapter.Fill(Me.DsSeries.tblProductList)
    Me.TblModelListTableAdapter.Fill(Me.DsModels.tblModelList)
    Me.TblItemMasterTableAdapter.Fill(Me.DsItemMaster.tblItemMaster)
    'Me.TblChartUsageTableAdapter.Fill(Me.DsChartEntry.tblChartUsage)

End Sub
Private Sub getyear()

    'Load default year based on boolean field in database
    Dim myear As String = ""

    For i = 0 To DsModelYear.tblModelYear.Rows.Count - 1

        If DsModelYear.tblModelYear.Rows(i)("Default").ToString = True Then
            myear = DsModelYear.tblModelYear.Rows(i)("ModelYear").ToString
            cmbYear.SelectedValue = myear
            schart.Year = myear
            Debug.Print(schart.Year)
            TblProductListBindingSource.Filter = "ModelYear = " & schart.Year
        End If

    Next

End Sub
Private Sub cmbSeries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSeries.SelectedIndexChanged

    'Filter Model List based on Series chosen
    schart.Series = cmbSeries.SelectedValue
    Debug.Print(schart.Series)
    TblModelListBindingSource.Filter = "ModelYear = " & schart.Year & " And SeriesID like '" & schart.Series & "'"

End Sub

Private Sub cmbModel_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbModel.SelectedIndexChanged

    schart.Model = cmbModel.SelectedValue
    Debug.Print(schart.Model)
    'Call gridfilter()

End Sub

Private Sub LoadGrid()

    'load dataset for year, series, and model selected - then filter for category selection
    'Selecting model triggers dataset to fill - increases performance this way.

    'Me.TblChartUsageTableAdapter.ClearBeforeFill = True
    Me.TblChartUsageTableAdapter.Fill(Me.DsChartEntry.tblChartUsage)
    'TblChartUsageBindingSource.Filter = "ModelIndex = '" & schart.Model & "'" & " And Category = '" & schart.Category & "'"

End Sub

Private Sub TabControl1_Selected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Selected

    schart.Category = TabControl1.SelectedTab.Text
    Debug.Print(schart.Category)
    Call gridfilter()

    'TblChartUsageBindingSource.Filter = "Category like '" & TabControl1.SelectedTab.Text & "'"
    'TblChartUsageBindingSource.Filter = "Category like '" & TabControl1.SelectedTab.Name & "'"

End Sub

Private Sub gridfilter()

    Debug.Print(schart.Year)
    Debug.Print(schart.Series)
    Debug.Print(schart.Category)
    Debug.Print(schart.Model)

    Dim fltr As String = ""
    Dim fcat As String
    Dim ftag As String = "*"

    If schart.Category = "Electrical 12v" Then
        fcat = "Electrical"
        ftag = "12V"
        fltr = "ModelIndex = '" & schart.Model & "'" & " And Category = '" & fcat & "'" & " And Tag = '" & ftag & "'"
    ElseIf schart.Category = "Electrical 120v" Then
        fcat = "Electrical"
        ftag = "120V"
        fltr = "ModelIndex = '" & schart.Model & "'" & " And Category = '" & fcat & "'" & " And Tag = '" & ftag & "'"
    Else fcat = schart.Category
        fltr = "ModelIndex = '" & schart.Model & "'" & " And Category = '" & fcat & "'"
    End If
    Debug.Print(fltr)

    TblChartUsageBindingSource.Filter = fltr

End Sub

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
    Call LoadGrid()
    Call gridfilter()

End Sub

'Edited to make code more readable

0 个答案:

没有答案