如何根据第一个选定的值填充第二个组合框 [英] How to fill the second combobox on depending on the selectedvalue of the first

查看:116
本文介绍了如何根据第一个选定的值填充第二个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用visual studio 2008开发一个应用程序,我正在尝试找到确切的代码来填充第二个组合框取决于我的mysql数据库中第一个组合框的选定值有两个表活动和组合。

这是我的代码:

'填充combobox1表活动
Sub fillCbbActivitychoice()
strsql =select * from activity
objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql,objconn)
objdr = objcmd.ExecuteReader
While(objdr.Read())
使用ComboBox1.Items.Add(objdr(Activity_Name))
结束
结束
objcmd.Dispose()
objdr.Close()
End Sub


'用表组填充combobox2
Sub fillCbbGpe()
strsql =select *来自groupe,其中Activity_Name ='& ComboBox1.Text&'
objcmd = New My Sql.Data.MySqlClient.MySqlCommand(strsql,objconn)
objdr = objcmd.ExecuteReader
While(objdr.Read())
使用ComboBox2.Items.Add(objdr(Libelle_Gpe)) )
结束
结束
objcmd.Dispose()
objdr.Close()
结束子

私人子Form4_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
fillCbbActivitychoice()
End Sub


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System .Object,ByVal e As System.EventArgs)处理ComboBox1.SelectedIndexChanged

strsql =select * from groupe where Activity_Name ='& ComboBox1.Text& '
objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql,objconn)
'使用objcmd
'.Parameters.AddWithValue(@ field3,ComboBox1.Text)
'结束于
objdr = objcmd.ExecuteReader
If(objdr.Read())= True然后
ComboBox2.Items.Add(objdr(Gpe_Name))
结束如果
objcmd.Dispose()
objdr.Close()
End Sub

解决方案

从组合框到组合框,你有一个成员值和显示值,当你将一些项添加到组合框但你不能用它们与

 ComboBox2.Items.Add(objdr(Libelle_Gpe))

所以你可以使用

 ComboBox1.DataSource 

但是在使用它之前不要忘记你应该初始化DisplayValue和MemberValue属性。



例如:



 strsql =  从T中选择id,Title able1 



 ComboBox1.DisplayMember =  标题 
ComboBox1.ValueMember = id
ComboBox1.DataSource = MyDataSource



您可以使用SqlDataAdapter对象填充数据源(MyDataSource),并通过查询获取数据。最后当你想要绑定第二个ComboBox时,你应该使用拳头ComboBox的SelectedValue属性,例如:

 strsql =  从groupe中选择*,其中Activity_Name ='& ComboBox1.SelectedValue&安培;  ' 



当然,如果你想要使用ComboBox.Text你可以使用它,但你应该改变你的数据库。



最好的问候。

Homay


Hi everybody,

I'm developping an application with visual studio 2008 and i'm trying to find the exact code to fill the second combobox on depending on the selected value of the first combobox from my mysql database with two tables activity and groupe.
This is my code:

'fill the combobox1 with table activity
   Sub fillCbbActivitychoice()
       strsql = "select * from activity
       objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn)
       objdr = objcmd.ExecuteReader
       While (objdr.Read())
           With ComboBox1.Items.Add(objdr("Activity_Name"))
           End With
       End While
       objcmd.Dispose()
       objdr.Close()
   End Sub


'fill the combobox2 with table groupe
    Sub fillCbbGpe()
        strsql = "select * from groupe where Activity_Name=' " & ComboBox1.Text & " '"
        objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn)
        objdr = objcmd.ExecuteReader
        While (objdr.Read())
            With ComboBox2.Items.Add(objdr("Libelle_Gpe"))
            End With
        End While
        objcmd.Dispose()
        objdr.Close()
    End Sub

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fillCbbActivitychoice()
    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        
        strsql = "select * from groupe where Activity_Name=' " & ComboBox1.Text & " '"
        objcmd = New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn)
        'With objcmd
        '.Parameters.AddWithValue("@field3", ComboBox1.Text)
        'End With
        objdr = objcmd.ExecuteReader
        If (objdr.Read()) = True Then
            ComboBox2.Items.Add(objdr("Gpe_Name"))
        End If
        objcmd.Dispose()
        objdr.Close()
    End Sub

解决方案

From to the combobox you have a Member value and Display value when you are add some item to the combobox but you can`t use them with

ComboBox2.Items.Add(objdr("Libelle_Gpe"))

so you can use

ComboBox1.DataSource

but don`t forget before than use it you should initial DisplayValue and MemberValue properties.

For Example:

strsql = "select id,Title from Table1 "


ComboBox1.DisplayMember = "Title"
ComboBox1.ValueMember = "id"
ComboBox1.DataSource = MyDataSource


and you can fill your data source (MyDataSource) with SqlDataAdapter object with fetching data by your query. finally when you want to the bind second ComboBox you should use SelectedValue properties of fist ComboBox for example:

strsql = "select * from groupe where Activity_Name=' " & ComboBox1.SelectedValue& " '"


Of course if you want use ComboBox.Text you can use it but, you should change something to the from of your database.

Best Regards.
Homay


这篇关于如何根据第一个选定的值填充第二个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆