list泛型绑定cbo出现无法绑定到新的显示成员
2012-11-12 15:11
190 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FlashDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.cbDepart = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.cbDepart.FormattingEnabled = true;
this.cbDepart.Location = new System.Drawing.Point(100, 55);
this.cbDepart.Name = "comboBox1";
this.cbDepart.Size = new System.Drawing.Size(121, 20);
this.cbDepart.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cbDepart);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ComboBox cbDepart;
private void Form1_Load(object sender, EventArgs e)
{
BindComBox();
this.cbDepart.SelectedValue="3";
}
private void BindComBox()
{
List <Depart>list = new List<Depart>();
Depart d1 = new Depart( "1", "销售部" );
Depart d2 = new Depart ( "2", "技术部" );
Depart d3 = new Depart ("3", "生产部" );
list.Add(d1);
list.Add(d2);
list.Add(d3);
cbDepart.DataSource = list;
cbDepart.DisplayMember = "DepartName";
cbDepart.ValueMember = "ID";
}
}
public class Depart
{
protected string _ID;
protected string _DepartName;
public
Depart(string ID, string DepartName)
{
this._ID = ID;
this._DepartName = DepartName;
}
public string ID
{
get
{
return this._ID;
}
set
{
_ID = value;
}
}
public string DepartName
{
get
{
return _DepartName;
}
set
{
_DepartName = value;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FlashDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.cbDepart = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.cbDepart.FormattingEnabled = true;
this.cbDepart.Location = new System.Drawing.Point(100, 55);
this.cbDepart.Name = "comboBox1";
this.cbDepart.Size = new System.Drawing.Size(121, 20);
this.cbDepart.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cbDepart);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ComboBox cbDepart;
private void Form1_Load(object sender, EventArgs e)
{
BindComBox();
this.cbDepart.SelectedValue="3";
}
private void BindComBox()
{
List <Depart>list = new List<Depart>();
Depart d1 = new Depart( "1", "销售部" );
Depart d2 = new Depart ( "2", "技术部" );
Depart d3 = new Depart ("3", "生产部" );
list.Add(d1);
list.Add(d2);
list.Add(d3);
cbDepart.DataSource = list;
cbDepart.DisplayMember = "DepartName";
cbDepart.ValueMember = "ID";
}
}
public class Depart
{
protected string _ID;
protected string _DepartName;
public
Depart(string ID, string DepartName)
{
this._ID = ID;
this._DepartName = DepartName;
}
public string ID
{
get
{
return this._ID;
}
set
{
_ID = value;
}
}
public string DepartName
{
get
{
return _DepartName;
}
set
{
_DepartName = value;
}
}
}
}
相关文章推荐
- list泛型绑定cbo出现无法绑定到新的显示成员
- WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
- 无法绑定到新的显示成员,参数名:newDisplayMember
- SenchaTouch2中list组件无法绑定存储或者绑定后仍旧无法显示数据问题解决
- WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
- !comboBox.ValueMember 出错 无法绑定到新的显示成员 参数名: newDisplayMember
- C# 错误:无法绑定到新的显示成员
- 关于list转json出现无法显示xml(XML解析错误:未组织好)
- 用List<T> 给 DataGridView 绑定数据时,无法正常显示
- [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
- Coco2d-x 3.x : Cocos Studio 制作的UI无法正确显示,出现绿色或白色底
- 当tableview滑到底部,点击cell出现的下拉框无法显示的问题
- asp.net绑定checkboxlist--------jquery绑定click事件,获取checkbox的value和显示值
- C#中出现“集合已修改;可能无法执行枚举操作”错误 foreach list.Remove()
- 解决在List中用Bitmap作Image的source时候无法正常显示的方法
- 关于win32绘图出现无法显示彩色问题的解决
- 请问如何解决WORD转HTML是出现的“内存或磁盘空间不足,无法显示或打印图片”这个问题?
- 解决用<img src="<%# Eval("ShowPhoto")%>"动态绑定图片,发布到IIS上无法显示的问题
- FileUpload上传文件出现Internet Explorer无法显示该页面
- 打开网站php出现乱码问题的解决办法以及无法显示验证码