首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么将winform中的listbox 与 hashtable绑定

2012-02-19 
如何将winform中的listbox 与 hashtable绑定System.Collections.Hashtable hash new System.Collections

如何将winform中的listbox 与 hashtable绑定
System.Collections.Hashtable hash = new System.Collections.Hashtable();
  for (int i = 0; i < 10; i++)
  {
  hash.Add(i.ToString(), "value:" + i.ToString());
  }
   
  //将HASH绑到LISTBOX  
  System.Collections.IDictionaryEnumerator myEnumerator = hash.GetEnumerator();
  while (myEnumerator.MoveNext())
  {  
  listBox1.Items.Add(myEnumerator.Value);
  }
  }

现在LISTBOX只能显示信息,不能将KEY绑到上面,而直接用下面语句,会出错
  this.listBox1.DataSource = hash;
  this.listBox1.DisplayMember = "Key";
  this.listBox1.ValueMember = "Value";

[解决办法]
使用结构体或类来做为ListBox的Items的元素,重写结构体或类的ToString方法显示文本。可以使用List<T>来存放Items并把List<T>绑到ListBox上,或通过循环添加到ListBoix上,取元素的时候对元素进行强制转换就可以了。比如:

C# code
        public class item        {            private object m_Value;            private string m_Text;            public item(object value, string text)            {                this.m_Value = value;                this.m_Text = text;            }            public object Value            {                get { return m_Value; }            }            public string Text            {                get { return m_Text; }            }            public override string ToString()            {                return this.m_Text;            }        }        protected override void OnLoad(EventArgs e)        {            base.OnLoad(e);            List<item> list = new List<item>();            for (int i = 0; i < 10; i++)            {                 list.Add(new item(i, "item" + i.ToString()));            }            this.listBox1.DataSource = list;        }        protected override void OnClick(EventArgs e)        {            base.OnClick(e);            item _item = this.listBox1.Items[this.listBox1.SelectedIndex] as item;            if (_item != null)            {                Console.WriteLine(_item.Value);            }        }
[解决办法]
C# code
 
System.Collections.Hashtable  hash  =  new  System.Collections.Hashtable();
for  (int  i  =  0;  i  <  10;  i++)
{
hash.Add(i.ToString(),  "value:"  +  i.ToString());
}
         
//将HASH绑到LISTBOX 
System.Collections.IDictionaryEnumerator  myEnumerator  =  hash.GetEnumerator();
DataTable dtTemp=new DataTable();
dtTemp.Columns.Add("key",typeof(string));
dtTemp.Columns.Add("value",typeof(string));
DataRow drTemp=null;
while (myEnumerator.MoveNext())
{             
drTemp=dtTemp.NewRow();
drTemp["key"]=myEnumerator.Key;
drTemp["value"]=myEnumerator.Value;
dtTemp.Rows.Add(drTemp);
}
this.listBox1.DataSource=dtTemp;
listBox1.DisplayMember="key";
listBox1.ValueMember="value";

[解决办法]
Hashtable has = new Hashtable();
for (int i = 0; i < 10; i++)
{
DataTable dt = new DataTable();
dt.Columns.Add("Key");
dt.Columns.Add("Val");
for (int j = 0; j < 10; j++)
{
DataRow r = dt.NewRow();
r["Key"] = "Tab" + i.ToString() + ",Key:" + j.ToString();
r["Val"] = "Tab" + i.ToString() + ",Val:" + j.ToString();
dt.Rows.Add(r);
}
has.Add("Tab" + i.ToString(),dt);
}
listBox1.DataSource = has["Tab1"];
listBox1.DisplayMember = "Val";
listBox1.ValueMember = "Key";
[解决办法]
从你的代码来看,不需要用HashTable,而且这样用肯定是不对的。我的建议如下。

C# code
class YourType{  int id  string val;    public int ID  {    get{return id;}    set{id = value;}  }    public string Value  {    get{return val;}    set{val = value;}  }}public class YourClass{  private List listBox1 = new ListBox();  public void SetBinding()  {     List<YourType> items = new List<YourType>();     for(int   i   =   0;   i   <   10;   i++)      {       YourType item = new YoutType();       item.ID = i;       item.Value = "Value: " + i;       items.Add(item);     }     listBox1.DataSource = items;     listBox1.DisplayMember = "Value";     listBox1.VauleMember = "ID";  }}
[解决办法]
建议你不要用HashTable,改用DataTable,这样绑定的时候,可以读到DisplayMember和ValueMember

热点排行