网易博客日志,看Hashtable如何绑定ComBox !~!~ - 程序人生的日志 - 网易博客

///最近做数据绑定,因为数据不是从数据库读取出来的,都是些不规范的数据
///所以绑定数据的时候出问题了,发现在WinForm 中 Hashtable不能直接绑定combox下拉框 !
///后来转换了一下,还算通过了,所以贴出来跟大家分享一下。
///
/// 另外,再罗嗦一点,我发现comBox绑定数据源后,不能直接修改 它的item 项,
/// 否则也会报错,要是你的下拉列表的元素不固定,或同时来自两个以上不同的数据源 ,
/// 则最好是一个一个的Item.Add()进去,不要用DataSource ,这样 你方便随时一个一个地Clear掉它的Item 。
///
///先创建一个WinForm应用程序,然后将默认的Form1和Program都删掉,再创建下面这个类就行了,下面的代码拷贝上去.
using System.Windows.Forms;
namespace Jianbing.ComboxBindingHashTable
{
public class Form1 : System.Windows.Forms.Form
{
//先制造出一个Hashtable,你可以从其他地方获取数据
private System.Collections.Hashtable myHash = null;
//用于转换后绑定下拉列表
private System.Collections.ArrayList list = new System.Collections.ArrayList( );
public Form1( )
{
InitializeComponent( );
}
///
/// 必需的设计器变量。
///

private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///

/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose( bool disposing )
{
if ( disposing && (components != null) )
{
components.Dispose( );
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///

private void InitializeComponent( )
{
this.comboBox1 = new System.Windows.Forms.ComboBox( );
this.SuspendLayout( );
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point( 66 , 82 );
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size( 272 , 20 );
this.comboBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F , 12F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size( 379 , 276 );
this.Controls.Add( this.comboBox1 );
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler( this.Form1_Load );
this.ResumeLayout( false );
}
#endregion
private System.Windows.Forms.ComboBox comboBox1;
///
/// 应用程序的主入口点。
///

[System.STAThread]
static void Main( )
{
Application.EnableVisualStyles( );
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form1( ) );
}
private void Form1_Load( object sender , System.EventArgs e )
{
cmbBoxBinding( );
}
//绑定
private void cmbBoxBinding( )
{
if ( myHash == null )
{
myHash = new System.Collections.Hashtable( );
myHash.Add( "JIANBING" , "我是中国人1" );
myHash.Add( "CH" , "我爱我的祖国2" );
myHash.Add( "U" , "你是傻大木3" );
myHash.Add( "M" , "我想找个MM谈恋爱4" );
myHash.Add( "MM" , "因为我想有人疼爱我5" );
myHash.Add( "MI" , "我想要个真心对我好的的女孩6" );
}
foreach ( System.Collections.DictionaryEntry entry in myHash )
list.Add( entry );
//绑定这该死的 下拉列表
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
}
}
}
///运行完后不知道大家发现没有,我本来想让它按我绑定的顺序显示的,
///即出现的顺序应该是“我是中国人”、"我爱我的祖国"、"你是傻大木"、 "我想谈恋爱了"……
///但不是这样子,因为Hashtable中的元素是按照里面元素的 哈稀码来排序的,所以出来的结果不是我
///想要的。在ArrayList中是按添加元素的先后来出现,问题出在foreach 循环处 !!
///把 cmbBoxBinding() 这个方法改一下:将下面代码拷贝上去替换原来的那个cmbBoxBinding()
///去掉Hashtable 对象,照样是hashTable的形式,嘿嘿 !~
//private void cmbBoxBinding()
// {
// list.Add( new System.Collections.DictionaryEntry( "JIANBING" , "我是中国人1" ) );
// list.Add( new System.Collections.DictionaryEntry( "CH" , "我爱我的祖国2" ) );
// list.Add( new System.Collections.DictionaryEntry( "U" , "你是傻大木3" ) );
// list.Add( new System.Collections.DictionaryEntry( "M" , "我想找个MM谈恋爱4" ) );
// list.Add( new System.Collections.DictionaryEntry( "MM" , "因为我想有人疼爱我5" ) );
// list.Add( new System.Collections.DictionaryEntry( "MI" , "我想要个真心对我好的的女孩6" ) );
// //绑定 下拉列表
// comboBox1.DataSource = list;
// comboBox1.DisplayMember = "Value";
// comboBox1.ValueMember = "Key";
// }
Tags:  网易博客日志权限 网易博客导入日志 网易博客日志

延伸阅读

最新评论

发表评论