如何使用窗口形式在C#中创建动态下拉列表 [英] how to create a dynamic drop down list in C# with window forms

查看:255
本文介绍了如何使用窗口形式在C#中创建动态下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为朋友手机商店构建一个简单的库存计划 我想使用C#并访问数据库来存储数据

数据库将有2个主要列表:

  1. 设备-品牌,型号,颜色,价格,库存,最低库存
  2. 零件-品牌,型号,说明,库存,最低库存

GUI将使用多个下拉列表 品牌会给我的:三星,htc等.... 模型会给我:I9000,i9100,ect ...

我试图思考如何制作时遇到的两个主要问题是:

  • 每次出现新的品牌和型号 因此,我需要一个选项来向数据库中添加新的型号/品牌,然后更新下拉列表以使其自动具有
  • 下拉列表需要连接 因此,如果我选择品牌A,则只有品牌A型号会出现在型号下拉列表中

解决方案

首先:不要使用Access,请使用SQL Server 2008 Express.但这只是个人喜好.

要实现所需的目标,需要执行的操作:在Visual Studio中创建一个类型化的数据集,其中包含一个品牌表和一个模型表.使用brand列上的关系将两个表关联起来.

从数据库中读取两个表-首先是brands表,然后是models表.否则,您会得到一个错误.

为两个列表创建一个主/详细绑定.这样做,将两个BindingSource实例拖放到您的窗体.将其中之一与品牌"列表关联,将其中之一与设备"列表关联. 品牌" BindingSource的数据源必须设置为数据集中的brands表,其他BindingSource的数据源必须设置为两个表之间的 relation .所有这些都可以在设计器中合而为一.

如果您在设置绑定时遇到问题,请使用Google搜索"C#主详细信息绑定数据集".

编辑
另一种选择 - 如果数据库连接是快 - 将填补从数据库只要选择一个品牌,清晰,使用所选择的品牌名称从上飞数据库重新填写型号列表品牌之列<. /p>

I'm trying to build a simple inventory program for a friends cellphone store I want to use C# and access database to store the data

the DB will have 2 main lists:

  1. devices - brand, model, color, price, stock,min_stock
  2. parts - brand, model, description,stock,min_stock

the GUI will use several drop lists brand will give me : samsung, htc, ect'.... model will give me : I9000,i9100,ect...

the two main problems I have when I try to think how to make it are this:

  • Each time new brands and models appear so i need an option to add a new model/brand to the database and then update the drop down list to have them automatically
  • The drop lists needs to be connected so if I choose brand A only brands A models will appear in the model drop list

解决方案

First of all: don't use Access, use SQL Server 2008 Express. But that's only personal taste.

What you need to do to achieve what you want: Create a typed dataset in Visual Studio that contains one table for brands and one for models. Associate the two tables using a relation on the brand column.

Read both tables from the database - first the brands table, then the models table. Otherwise you'll get an error.

Create a master/detail binding for both lists. To do so, drop two BindingSource instances to your form. Associate one of them to the "brands" list and one to the "devices" list. The datasource for the "brands" BindingSource must be set to the brands table in the dataset, the datasource for the other BindingSource must be set to the relation between the two tables. All of this can be one in the designer.

If you have problems setting up the bindings, google for "C# master detail binding dataset".

EDIT
Another option - if the database connection is fast - would be to fill the list of brands from the database and whenever a brand is selected, clear and re-fill the list of models from the database on the fly using the selected brand name.

这篇关于如何使用窗口形式在C#中创建动态下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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