绑定源 [英] Binding Source
本文介绍了绑定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好.....
我现在正在开发app。其中包含带有SQL Server的设计和数据库
我有一个组合框,我想用一个字段填充它从我的数据库我现在想要怎样才能做到这一点....? 写下代码或者给我任何参考来学习
谢谢
解决方案
这是一个例子 - >
class Country
{
public Country(字符串值)
{
_name = value;
}
私人字符串_name;
公共字符串名称
{
get
{return _name; }
设定
{_name = value; }
}
}
列表<国家> all = new List<国家> ();
all.Add(新国家(X));
all.Add(新国家(Y));
all.Add(new Country(Z));
//您可以认为all是数据库表中的数据列表Country&Name是表的一列。
comboBox1.DataSource = all;
comboBox1.DisplayMember =Name;
comboBox1.ValueMember =Name;
comboBox1.SelectedIndex = 0;
< br />
DataSet ds = new DataSet();< br />
< br />
SqlConnection connection = new SqlConnection(@Data Source = HEINZZ-PC\SQLEXPRESS; Initial Catalog = MyDB; Integrated Security = True);< br />
SqlDataAdapter da = new SqlDataAdapter();< br />
da.SelectCommand = new SqlCommand(SELECT * FROM Personel,connection);< br />
da.Fill(ds) ;< br />
< br />
this.comboBox1.DataSource = ds.Tables [0];< br />
this.comboBox1.DisplayMember =Name ;< br />
datagridview绑定源3layer [ ^ ]
Hi There.....
I now develop app. that contains Design and database with SQL Server
and i have a combo Box that i want to fill it with a Field From my data base what i want now how can i do that ....?Please write down the code or give me any reference to learn that
thanks
解决方案
Here is an example ->
class Country
{
public Country(string value)
{
_name = value;
}
private string _name;
public string Name
{
get
{ return _name; }
set
{ _name = value; }
}
}
List < country > all = new List < country > ();
all.Add(new Country("X"));
all.Add(new Country("Y"));
all.Add(new Country("Z"));
//you can consider all is a list of data from your database table Country & Name is a column of the table.
comboBox1.DataSource = all;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";
comboBox1.SelectedIndex = 0;
<br /> DataSet ds = new DataSet();<br /> <br /> SqlConnection connection = new SqlConnection(@"Data Source=HEINZZ-PC\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True");<br /> SqlDataAdapter da = new SqlDataAdapter();<br /> da.SelectCommand = new SqlCommand("SELECT * FROM Personel", connection);<br /> da.Fill(ds);<br /> <br /> this.comboBox1.DataSource = ds.Tables[0];<br /> this.comboBox1.DisplayMember = "Name";<br />
datagridview binding source 3layer[^]
这篇关于绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文