连接两个ComboBox [英] Conecting two ComboBoxs
问题描述
我在Visual Studio中使用Windows窗体应用程序制作了一些项目。
我已将访问日期库连接到ComboBox1和ComboBox2(第一列到ComboBox1;第二列到ComboBox2)  但如果我扩展名单,我会看到每条记录。
我应该写什么/做什么 当我想在ComboBox2上只记录来自Access第一栏的记录时:
纸 - 蓝色¥b $ b $
金属 - 绿色
木材 - 橙色
纸张 - 绿色¥b $ b
因此,如果我选择ComboBox1纸张,我将使用ComboBox2:
- 蓝色
- 绿色
你好,
由于从ms-access表中读取数据会产生DataTable(当然你可以读入DataSet但是每个表都会生成一个DataTable,所以我们回到了DataTable)我模拟了下面的表。
'''< summary>
'''模拟数据来自ms-access
'''< / summary>
公共类DataOperations
公共函数ReadTable1()As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn()With {.ColumnName =" Id" ,.DataType = GetType(Integer)})
dt.Columns.Add(New DataColumn()With {.ColumnName =" Value",。DataType = GetType(String)})
dt.Rows.Add(1," Paper")
dt.Rows.Add(2," Metal")
dt.Rows.Add(3," Wood")
dt.Rows.Add(4," Green")
返回dt
结束函数
公共函数ReadTable2()As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn()With {.ColumnName =" Id",。DataType = GetType(Integer)})
dt.Columns.Add(New DataColumn ()使用{.ColumnName =" Table1Id",。DataType = GetType(Integer)})
dt.Columns.Add(New DataColumn()With {.ColumnName =" Value" ,. DataType = GetType(String)})
dt.Rows.Add(1,1," Blue")
dt.Rows.Add(2,2," Orange")
dt.Rows.Add(3,1," Green")
dt.Rows.Add(4,3," Blue")
dt.Rows.Add(5, 3,"巧克力")
dt.Rows.Add(6,4," Blue")
dt.Rows.Add(7,4," Purple")
返回dt
结束功能
结束等级
表格代码
< pre class ="prettyprint lang-vb"> Public Class Form1
Private Table2 As New DataTable
Private Sub Form1_Load(sender as Object,e As EventArgs)Handles MyBase.Load
Dim ops = New DataOperations
Table2 = ops.ReadTable2
ComboBox1.DataSource = ops.ReadTable1
ComboBox1.DisplayMember =" Value"
ComboBox1.ValueMember =" Id"
AddHandler ComboBox1.SelectedIndexChanged,AddressOf ComboBox1_SelectedIndexChanged
ComboBox2.DisplayMember =" Value"
ComboBox2.ValueMember =" Table1Id"
HandleChanges()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object,e As EventArgs)
HandleChanges()
End Sub
Private Sub HandleChanges()
Table2.DefaultView.RowFilter =
" Table1Id = {CInt(ComboBox1.SelectedValue)}"
ComboBox2.DataSource = Table2
End Sub
End Class
I making some project using Windows Forms Application in Visual Studio.
I have conected Access date base to ComboBox1 and ComboBox2 (First column to ComboBox1; 2nd column to ComboBox2) but if I expand the list I see every record.
What should I write/do when I want to have on ComboBox2 only record from Access conected with first column:
Paper - Blue
Metal - Green
Wood - Orange
Paper - Green
So if I chose in ComboBox1 Paper I will be have in ComboBox2:
- Blue
- Green
Hello,
Since reading data from a ms-access table results in a DataTable (of course you can read into a DataSet but each table results in a DataTable so we are back to a DataTable) I mocked up to tables below.
''' <summary> ''' Mocked data that would come from ms-access ''' </summary> Public Class DataOperations Public Function ReadTable1() As DataTable Dim dt As New DataTable dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer)}) dt.Columns.Add(New DataColumn() With {.ColumnName = "Value", .DataType = GetType(String)}) dt.Rows.Add(1, "Paper") dt.Rows.Add(2, "Metal") dt.Rows.Add(3, "Wood") dt.Rows.Add(4, "Green") Return dt End Function Public Function ReadTable2() As DataTable Dim dt As New DataTable dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer)}) dt.Columns.Add(New DataColumn() With {.ColumnName = "Table1Id", .DataType = GetType(Integer)}) dt.Columns.Add(New DataColumn() With {.ColumnName = "Value", .DataType = GetType(String)}) dt.Rows.Add(1, 1, "Blue") dt.Rows.Add(2, 2, "Orange") dt.Rows.Add(3, 1, "Green") dt.Rows.Add(4, 3, "Blue") dt.Rows.Add(5, 3, "Chocolate") dt.Rows.Add(6, 4, "Blue") dt.Rows.Add(7, 4, "Purple") Return dt End Function End ClassForm code
Public Class Form1 Private Table2 As New DataTable Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim ops = New DataOperations Table2 = ops.ReadTable2 ComboBox1.DataSource = ops.ReadTable1 ComboBox1.DisplayMember = "Value" ComboBox1.ValueMember = "Id" AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged ComboBox2.DisplayMember = "Value" ComboBox2.ValueMember = "Table1Id" HandleChanges() End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) HandleChanges() End Sub Private Sub HandleChanges() Table2.DefaultView.RowFilter =
"Table1Id = {CInt(ComboBox1.SelectedValue)}" ComboBox2.DataSource = Table2 End Sub End Class
这篇关于连接两个ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!