如何在组合框中进行过滤 [英] how to filter in combo box

查看:57
本文介绍了如何在组合框中进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我的程序中有两个组合框

例如,如果您单击第一个组合框,则有一个子团队,项目将是
A1
A2
A3
A4
A5
我想发生的是,如果我选择A1,则只有A1团队的成员会出现在我的组合框2中.这样,您需要一条sql语句.但是我该怎么编码呢?有人可以帮我吗?

[Addition from OP - posted as answer - answer deleted]

 dim sql  as  字符串
sql = " & cboselect.Text& " 
如果cboselect.SelectedIndex =  0 ,则cboselect2.Items.Add(sql)



我尝试过这个但是

每当我执行程序时,第二个组合框中的数据都是从finaltickettable中选择*,其中Subteam ="& cboselect.Text&

解决方案

需要这样的SQL查询:-

  SELECT 字段1,字段2,字段3  FROM  yourTable  @ selectedTeam ; 




您将必须使用自己的方法将SQL放入数据库查询中,并在组合选择的项目发生更改时调用此方法.像这样的东西:-

 私有 无效 comboBox1_SelectedIndexChanged(对象发​​件人,EventArgs e)
        {
            GetTeamMembers(comboBox1.SelectedText);
        }

        无效 GetTeamMembers(字符串子团队)
        {
            使用(OleDbConnection con =  OleDbConnection("  yourConnectionString"))
            {
                OleDbCommand cmd = 新建 OleDbCommand(" ,骗子);
                cmd.Parameters.AddWithValue(" ,subTeam);
                OleDbDataReader dr = cmd.ExecuteReader();
                List< string> teamMembers =  List< string>();
                 while (dr.Read())
                {
                    comboBox2.Items.Add(( string )dr [ 0 ]);
                }
            }
        } 



请不要只是一个例子.理想情况下,您的用户界面中永远不应包含数据访问代码.最好您有一个描述团队的类,并且数据访问方法应该在该类中,并返回该团队中的所有团队成员.

[/Edited]


这是vb.net的吗?我为我的程序使用vb.net我认为这不是vb.net.请给我vb.net代码.谢谢


 如果  TypeOf  cboselect.SelectedItem  字符串 然后
             Dim 连接 As  新建 MySqlConnection(connStr)
             Dim 命令 As  MySqlCommand = Connection.CreateCommand
             Dim 阅读器 As  MySqlDataReader = 没什么
            Command.CommandText = " & vbCrLf& _
            " & vbCrLf& _
            " 
            Command.Parameters.AddWithValue("  CStr (cboselect.SelectedItem))
            cboselect2.BeginUpdate()
            cboselect2.Items.Clear()
            尝试
                Connection.Open()
                读者= Command.ExecuteReader
                执行 阅读器
                    cboselect2.Items.Add( CStr (Reader(" )))
                循环
            捕获,例如 As 异常
            最后
                Connection.Dispose()
                Command.Dispose()
                ' 处置可能不适用于sqldatareader 
                如果阅读器是不是 没什么 然后 Reader.Dispose()
            结束 尝试
            cboselect2.EndUpdate()
        结束 如果 



这个工作了.


hi all! i have two combo box in my program

one has the subteam for example if you click the 1st combo box, the items would be
A1
A2
A3
A4
A5
what i want to happen is that if i chose A1 only the members of team A1 would appear in my combo box2. and by that, you need an sql statement. but how do i code that? can someone help me?

[Addition from OP - posted as answer - answer deleted]

dim sql as string
sql = "Select * from finaltickettable where Subteam = '" & cboselect.Text & "'"
If cboselect.SelectedIndex = 0 Then cboselect2.Items.Add(sql)



i tried this one but

whenever i execute the program, the data in my 2nd combo box is Select * from finaltickettable where Subteam = ''" & cboselect.Text &

解决方案

You will need an SQL query something like This :-

SELECT field1, field2, field3 FROM yourTable WHERE team = @selectedTeam;



[Edited]
You are going to have to put the SQL in a query to the database in a method of it''s own, and call this method whenever the combo selected item is changed. Something like this:-

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetTeamMembers(comboBox1.SelectedText);
        }

        void GetTeamMembers(string subTeam)
        {
            using (OleDbConnection con = new OleDbConnection("yourConnectionString"))
            {
                OleDbCommand cmd = new OleDbCommand("Select * from finaltickettable where Subteam = @subTeam", con);
                cmd.Parameters.AddWithValue("@subTeam", subTeam);
                OleDbDataReader dr = cmd.ExecuteReader();
                List<string> teamMembers = new List<string>();
                while (dr.Read())
                {
                    comboBox2.Items.Add((string)dr[0]);
                }
            }
        }



Please not this is just an example. Ideally, you should never have data access code in your user interface. Prefereably you should have a class that describes a Team and the data access method should be in that class and returns all the team members in that team.

[/Edited]


is this for vb.net? im using vb.net for my program i think this is not vb.net. kindly give me vb.net codes. thanks


If TypeOf cboselect.SelectedItem Is String Then
            Dim Connection As New MySqlConnection(connStr)
            Dim Command As MySqlCommand = Connection.CreateCommand
            Dim Reader As MySqlDataReader = Nothing
            Command.CommandText = "Select Distinct user" & vbCrLf & _
            "From finaltickettable" & vbCrLf & _
            "Where Subteam = @SubTeam"
            Command.Parameters.AddWithValue("@SubTeam", CStr(cboselect.SelectedItem))
            cboselect2.BeginUpdate()
            cboselect2.Items.Clear()
            Try
                Connection.Open()
                Reader = Command.ExecuteReader
                Do While Reader.Read
                    cboselect2.Items.Add(CStr(Reader("user")))
                Loop
            Catch ex As Exception
            Finally
                Connection.Dispose()
                Command.Dispose()
                'dispose may not be available with sqldatareader
                If Reader IsNot Nothing Then Reader.Dispose()
            End Try
            cboselect2.EndUpdate()
        End If



THIS ONE WORKED.


这篇关于如何在组合框中进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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