关于.NET中WinForms里面的ListBox实现数据绑定的...
点击次数:52 次 发布日期:2008-11-06 08:06:43 作者:源代码网
|
源代码网推荐 源代码网推荐-------------------------------------------------------------------------------- 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐在.NET中,WINDOW FORMS下面的LIST BOX控件在开发时,如果采用其本身的数据绑定,绑定完以后就不能更改ListBox的Items了.而实际开发中却经常会碰到要改变的情况,在这里我提供了一重方法.采用开发继承ListBox控件的自定义控件.然后在里面提供两个SortedList类的属性,一个可以存放ID,一个存放TEXT,这样就解决了上面说的问题!! 源代码网推荐 源代码网推荐控件的代码如下: 源代码网推荐 源代码网推荐using System; 源代码网推荐using System.Collections; 源代码网推荐using System.ComponentModel; 源代码网推荐using System.Drawing; 源代码网推荐using System.Data; 源代码网推荐using System.Windows.Forms; 源代码网推荐 源代码网推荐namespace FlowManage 源代码网推荐{ 源代码网推荐/// <summary> 源代码网推荐/// SysListBox 的摘要说明。 源代码网推荐/// </summary> 源代码网推荐public class SysListBox : System.Windows.Forms.ListBox 源代码网推荐{ 源代码网推荐 private SortedList _sl=new SortedList(); 源代码网推荐 /// <summary> 源代码网推荐 /// 必需的设计器变量。 源代码网推荐 /// </summary> 源代码网推荐 private System.ComponentModel.Container components = null; 源代码网推荐 源代码网推荐 public SysListBox() 源代码网推荐 { 源代码网推荐 // 该调用是 Windows.Forms 窗体设计器所必需的。 源代码网推荐 InitializeComponent(); 源代码网推荐 源代码网推荐 // TODO: 在 InitializeComponent 调用后添加任何初始化 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 /// <summary> 源代码网推荐 /// 清理所有正在使用的资源。 源代码网推荐 /// </summary> 源代码网推荐 protected override void Dispose( bool disposing ) 源代码网推荐 { 源代码网推荐 if( disposing ) 源代码网推荐 { 源代码网推荐 if(components != null) 源代码网推荐 { 源代码网推荐 components.Dispose(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 base.Dispose( disposing ); 源代码网推荐 } 源代码网推荐 源代码网推荐 public SortedList DataValues 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 return _sl; 源代码网推荐 } 源代码网推荐 set 源代码网推荐 { 源代码网推荐 _sl=value; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 public void AddItem(object key,object text) 源代码网推荐 { 源代码网推荐 if(this.DataValues==null) 源代码网推荐 { 源代码网推荐 this.DataValues=new SortedList(); 源代码网推荐 } 源代码网推荐 this.DataValues.Add(key,text); 源代码网推荐 } 源代码网推荐 public void RemoveItem(int index) 源代码网推荐 { 源代码网推荐 this.DataValues.RemoveAt(index); 源代码网推荐 } 源代码网推荐 public void RemoveItem() 源代码网推荐 { 源代码网推荐 this.DataValues.Clear(); 源代码网推荐 } 源代码网推荐 public void BoundList() 源代码网推荐 { 源代码网推荐 this.Items.Clear(); 源代码网推荐 if(this.DataValues!=null) 源代码网推荐 { 源代码网推荐 this.BeginUpdate(); 源代码网推荐 for(int i=0;i<this.DataValues.Count;i ) 源代码网推荐 { 源代码网推荐 this.Items.Add(this.DataValues.GetByIndex(i).ToString()); 源代码网推荐 } 源代码网推荐 this.EndUpdate(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 #region Component Designer generated code 源代码网推荐 /// <summary> 源代码网推荐 /// 设计器支持所需的方法 - 不要使用代码编辑器 源代码网推荐 /// 修改此方法的内容。 源代码网推荐 /// </summary> 源代码网推荐 private void InitializeComponent() 源代码网推荐 { 源代码网推荐 components = new System.ComponentModel.Container(); 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐} 源代码网推荐} 源代码网推荐而在调用这个控件时的代码如下: 源代码网推荐 源代码网推荐string mkey=this.listCanSel.DataValues.GetKey(this.listCanSel.SelectedIndex).ToString(); 源代码网推荐 源代码网供稿. |
