单击listBox中的项目时如何填充相应的sql数据 [英] How to populate corresponded sql data when I click an item in listBox

查看:74
本文介绍了单击listBox中的项目时如何填充相应的sql数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为People的sql表,它有Gender,FirstName和LastName。在我的visual studio项目中,我有两个列表框。一个用于性别,第二个用于名字和姓氏。我想在第一个ListBox中显示性别(男性和女性)。我已经完成了这个没有问题。我使用的代码如下。

I have a sql table called People and it has Gender, FirstName and LastName. In my visual studio project, I have two list boxes. One is for gender and the second one is for First and Last Name. I would like to display genders (Male and Female) in the first ListBox. I've done this w/out no problem. The code that I used is below.

SqlConnection cs = new SqlConnection("Data Source=ITPC;Initial Catalog=alumni;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("SELECT [Class] FROM [alumni].[dbo].[Class]", cs);
            DataTable dt = new DataTable();
            da.Fill(dt);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                listBoxClass.Items.Add(dt.Rows[i]["Class"]);
            }





所以对于第二个listBox,我想在点击男性项目和女性时显示男性名字单击第一个列表框中的女性项目时的名称。此外,如果我选择男性和女性项目,第二个列表框应该一起显示男性和女性名称。简单如果/不起作用。它应该是另一种类似的方式。我想知道如何在c#中完成这项工作。



So for the second listBox, I want to display the male names when I click "Male" item and female names when I click on "Female" item on the first List box. Also, If I select Male and Female items, the second listbox should be displaying male and female names together. Simple if/else did not work. It should be another and simle way to do it. I wonder how to accomplish this job in c#.

推荐答案

这是一个想法:

如何使用C#和VB.Net在WinForms Windows窗体应用程序中依赖于彼此创建级联组合框 [< a href =http://csharpdotnetfreak.blogspot.com/2012/07/cascading-combobox-in-winforms-windows-forms.htmltarget =_ blanktitle =New Window> ^ ]

C-Sharp.Net中的级联组合框 [< a href =http://bytesofcode.hubpages.com/hub/Cascading-ComboBox-in-C-SharpNet\"target =_ blanktitle =New Window> ^ ]
Here is an idea:
How To Create Cascading ComboBox Dependent On One Another In WinForms Windows Forms Applications Using C# And VB.Net[^]
Cascading ComboBox in C-Sharp.Net[^]


这篇关于单击listBox中的项目时如何填充相应的sql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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