在WINFORM中如何实现ComboBox于Hashtable的绑定? WinForm中下拉框为ComboBox,没有绑定的概念,你可以自己定义一个类作为ComboBox的Item,类中写两个属性,一为Text,一位Value,然后从写ToString()方法。即可存储Text和Value 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你一段代码:先定义一个类: public class Titles { private string titleid,title; public Titles(string titleid,string title) { this.titleid=titleid; this.title=title; } public string TitleID { get{return titleid;} set{titleid=value;} } public string Title { get{return title;} set{title=value;} } }应用代码: string sConnString="Server=(local);DataBase=pubs;UID=sa;PWD="; SqlConnection cn=new SqlConnection(sConnString); cn.Open(); string sSQL="SELECT * FROM Titles"; SqlCommand cm=new SqlCommand(sSQL,cn); SqlDataReader dr=cm.ExecuteReader(); Hashtable ht=new Hashtable(); Titles[] titleColl; while(dr.Read()) { ht.Add(dr["title_id"].ToString(),dr["title"].ToString()); } titleColl=new Titles[ht.Count]; int i=0; IDictionaryEnumerator enumerator=ht.GetEnumerator(); while(enumerator.MoveNext()) { titleColl[i]=new Titles(enumerator.Key.ToString(),enumerator.Value.ToString()); i++; } comboBox1.DataSource=titleColl; comboBox1.DisplayMember="Title"; comboBox1.ValueMember="TitleID"; wince 登录加密 tab键上面那个键叫什么名字啊? Access里的查询语句问题,请教!急......... 跪求Listbox的一个问题! c# 以只读模式连接数据库SQL Server 怎么来判断一行C#文字是类名,属性名,函数名,还是变量名?大家多多的帮忙哦 如何开发监控程序? 还是Treeview的问题,在线等待(先谢了) 如何设定控件的坐标? C#可不可以将应用程序做成系统服务运行? 对CSDN网站有些建议,大家讨论一下 急救!!!DataTable如何在当前行更新数据?
先定义一个类:
public class Titles
{
private string titleid,title;
public Titles(string titleid,string title)
{
this.titleid=titleid;
this.title=title;
}
public string TitleID
{
get{return titleid;}
set{titleid=value;}
}
public string Title
{
get{return title;}
set{title=value;}
}
}
应用代码:
string sConnString="Server=(local);DataBase=pubs;UID=sa;PWD=";
SqlConnection cn=new SqlConnection(sConnString);
cn.Open();
string sSQL="SELECT * FROM Titles";
SqlCommand cm=new SqlCommand(sSQL,cn);
SqlDataReader dr=cm.ExecuteReader();
Hashtable ht=new Hashtable(); Titles[] titleColl;
while(dr.Read())
{
ht.Add(dr["title_id"].ToString(),dr["title"].ToString());
}
titleColl=new Titles[ht.Count];
int i=0;
IDictionaryEnumerator enumerator=ht.GetEnumerator();
while(enumerator.MoveNext())
{
titleColl[i]=new Titles(enumerator.Key.ToString(),enumerator.Value.ToString());
i++;
}
comboBox1.DataSource=titleColl;
comboBox1.DisplayMember="Title";
comboBox1.ValueMember="TitleID";