在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"; C#接口是什么意思 ado.net有木有办法执行这种一条sql中涉及两个数据库的查询? Oledb查询Access数据库中备注类型被截断问题 visual studio 2008 分为x86和x64版吗? 问类的问题 关于c# winform中@符号使用的疑惑!! C#操作EXCEL 我是刚学C# 请大家多帮助 listbox如何设置行高? 还有如何在每一行的文字前加一个图标? [求助]在创建分布式应用程序时遇到的关于Web 引用的问题 请问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";