vbnet多个组合框填充一个数据集

时间:2012-06-06 16:30:54

标签: vb.net combobox dataset

我有以下代码使用一个数据集填充两个组合框:

Private Sub sub_cbo_type_load()
    Dim ds As New DataSet
    ds = cls.cbo_type()
    If ds IsNot Nothing _
    AndAlso ds.Tables.Count > 0 _
    AndAlso ds.Tables(0).Rows.Count > 0 Then
        Me.r_cbo_type.DataSource = ds.Tables(0)
        Me.r_cbo_type.DisplayMember = "desc"
        Me.r_cbo_type.ValueMember = "code"
        Me.r_cbo_type.SelectedIndex = -1
        Me.m_cbo_type.DataSource = ds.Tables(0)
        Me.m_cbo_type.DisplayMember = "desc"
        Me.m_cbo_type.ValueMember = "code"
        Me.m_cbo_type.SelectedIndex = -1
    End If
End Sub

问题是:只要在一个组合框中更改索引,它就会在另一个组合框中自动更改。

有谁知道如何解决这个问题?

谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

尝试克隆表:

Private Function CopyTable(ByVal sourceTable As DataTable) As DataTable
  Dim newTable As DataTable = sourceTable.Clone
  For Each row As DataRow In sourceTable.Rows
    newTable.ImportRow(row)
  Next
  Return newTable
End Function

然后您的数据源将引用不同的来源:

Me.r_cbo_type.DataSource = CopyTable(ds.Tables(0))

Me.m_cbo_type.DataSource = CopyTable(ds.Tables(0))

答案 1 :(得分:-1)

喜欢这个

 Private Sub btn_update1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_update1.Click
        If MsgBox("Are you sure to update?" & "", MsgBoxStyle.YesNo, "Confirmation") = MsgBoxResult.Yes = True Then
            Dim transmode As String = vbNullString
            Dim byair As String = vbNullString
            Dim bysea As String = vbNullString
            If rb_air.Checked = True Then
                transmode = "A"
                byair = txt_mserial.Text '.Substring(txt_mserial.TextLength - 4, 4)
                bysea = vbNullString
            ElseIf rb_sea.Checked = True Then
                transmode = "B"
                byair = vbNullString
                bysea = txt_mserial.Text '.Substring(txt_mserial.TextLength - 4, 4)
            End If
            Try
                If con.State = ConnectionState.Closed Then con.Open()
                global_command = New SqlCommand("update ytmi_finished_products set rev_ctrl_no = '" & txt_mrev.Text & "', by_air = '" & byair & "', by_sea = '" & bysea & "', transport_mode = '" & transmode & "' where REPLACE(prod_no, '-', '') +'-'+ ISNULL(CONVERT(varchar(50), prod_sx), '') + prod_lvl = '" & txt_mpart.Text & "' and cast(serial_no as numeric) = '" & txt_mserial.Text & "' and req_box_qty = '" & txt_mqty.Text & "' and remarks is null", con)
                global_command.ExecuteNonQuery()
                global_command.Dispose()

                MsgBox("Successfully Updated!", MsgBoxStyle.Information, "Message")
                mclear()
            Catch ex As Exception
                MsgBox("Trace No 20: System Error or Data Error!" + Chr(13) + ex.Message + Chr(13) + "Please Contact Your System Administrator!", vbInformation, "Message")
            End Try
        End If
    End Sub