如何从SQL数据表填充组合框 [英] How do I Populate a Combobox from a SQL Datatable

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

问题描述

非常基本的问题我希望...我正在尝试创建一个Windows窗体,允许用户从ComboBox中选择一个联盟,然后根据他在第二个ComboBox中选择的内容发送E:Mails。



它不能通过网络使用,只能在一个人的PC上使用,所以我不必担心大量的安全问题。



我设计了Form并且我有一个非常基本的SQL数据库作为Project的一部分,该表单包含一个BindingSource,一个BindingNavigator和一个TableAdapter。



问题是,如何使用Leagues DataSet中的数据填充第一个ComboBox?我可以这样做,这些数据是免费提供还是我仍然需要沿着创建ConnectionString和所有DataTable,DataSet,DataAdapter变量等的路径走下去?



VS2010会自动给我以下代码...



Pretty basic question I hope ... I am trying to create a Windows Form that will allow the user to choose a League from a ComboBox and then send E:Mails depending on what he chooses in a second ComboBox.

It is not for use over a network, just on one person's PC so I don't have to worry about massive security issues.

I have designed the Form and I have a very basic SQL database as part of the Project, the form has a BindingSource, a BindingNavigator and a TableAdapter all included.

The question is, how do I populate the first ComboBox with the data that is in the Leagues DataSet ? Can I do this, is this data freely available or do I still have to go down the path of creating a ConnectionString and all the DataTable, DataSet, DataAdapter variables etc., etc. ?

VS2010 automatically gives me the following code ...

Private Sub KAmail_Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'DB_KAmailDataSet.Leagues' table. You can move, or remove it, as needed.
    Me.LeaguesTableAdapter.Fill(Me.DB_KAmailDataSet.Leagues)
End Sub





...我可以做一些类似于用联盟名称填充ComboBox的东西吗?以下代码为每个联盟添加了1行到ComboBox,但它不是条目的值,它是一个文本行,上面写着KAmail.DB_KAmailDataSet + LeaguesRow...





... can I do something similar to "Fill" the ComboBox with the League Names ? The following code adds 1 line per League to the ComboBox, but it's not the Value of the entry, it is a text line that says "KAmail.DB_KAmailDataSet+LeaguesRow" ...

For MyIx = 0 To DB_KAmailDataSet.Leagues.Count - 1
    KAmail_ComboBox1.Items.Add(Me.DB_KAmailDataSet.Leagues.Item(MyIx))
Next





这是我用过的基于我的工作的CodeProject文章...... 创建.NET数据库应用程序的最快方法 [ ^ ]

推荐答案

设置数据组合框的rce是从数据层返回的对象列表。然后将此displaymember设置为具有要显示的字符串的成员,或者只是覆盖ToString以呈现所需的值。然后,所选项目将成为您的项目,并且一切正常。
Set the datasource of your combobox to be list of objects returned from your data layer. then set this displaymember to be the member that has the string to show, or just override ToString to rendedr the value you want. Then the selected item will be your item, and it will all work fine.


这篇关于如何从SQL数据表填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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