C# WinForm关于combobox的设置取值疑惑
刚用C#2天就晕倒了,做了一个界面。控件初始化做好了。现在,在combobox取值上面遇到问题了,
始终很疑惑,希望明白人能否解答一下疑问?
按照网上好多说的方法,通过对象设置值。
public class ListItem
{
private string id = string.Empty;
private string name = string.Empty; public ListItem(string sid, string sname)
{
id = sid; name = sname;
} public override string ToString()
{
return this.name;
} public string ID
{
get { return this.id; }
set { this.id = value; }
} public string Name
{
get { return this.name; }
set { this.name = value; }
} }
ListItem item = new ListItem("我是值", "我是名字");
this.cmb.Items.Add(item);
this.cmb.DisplayMember = "Name";
this.cmb.ValueMember = "ID";
这是网上很多人推荐的方法,但是按照这种方法根本无法取到valueMember的值。
我debug发现,cmb.SelectedValue始终都是null。而cmb.selectedItem取到的就是name的值,实际取的是覆盖了的toString()方法的值。
我对DisplayMember和ValueMember的设置始终不解。因为,在debug下,它不过就是2个字符串,并没有跟ListItem的Name和ID关联上,
尝试这样也不行
this.cmb.DisplayMember = item.Name.ToString()
this.cmb.ValueMember = item.ID.ToString();
而且,如果是对象数组,多个对象,值会被最后一个覆盖
晕,有明白的吗?谢谢
刚用C#2天就晕倒了,做了一个界面。控件初始化做好了。现在,在combobox取值上面遇到问题了,
始终很疑惑,希望明白人能否解答一下疑问?
按照网上好多说的方法,通过对象设置值。
public class ListItem
{
private string id = string.Empty;
private string name = string.Empty; public ListItem(string sid, string sname)
{
id = sid; name = sname;
} public override string ToString()
{
return this.name;
} public string ID
{
get { return this.id; }
set { this.id = value; }
} public string Name
{
get { return this.name; }
set { this.name = value; }
} }
ListItem item = new ListItem("我是值", "我是名字");
this.cmb.Items.Add(item);
this.cmb.DisplayMember = "Name";
this.cmb.ValueMember = "ID";
这是网上很多人推荐的方法,但是按照这种方法根本无法取到valueMember的值。
我debug发现,cmb.SelectedValue始终都是null。而cmb.selectedItem取到的就是name的值,实际取的是覆盖了的toString()方法的值。
我对DisplayMember和ValueMember的设置始终不解。因为,在debug下,它不过就是2个字符串,并没有跟ListItem的Name和ID关联上,
尝试这样也不行
this.cmb.DisplayMember = item.Name.ToString()
this.cmb.ValueMember = item.ID.ToString();
而且,如果是对象数组,多个对象,值会被最后一个覆盖
晕,有明白的吗?谢谢
解决方案 »
- 谁给俺把SQL改成access 多谢
- C# ACCESS 保留字
- vs2005+sqlserver2000开发的c/s架构的windows程序的运行环境由什么要求:客户端硬件配置和服务器配置各是什么?
- 请求帮忙!!!!用vb或vc进行编程获取本机安装的以太网卡的物理地址.非常谢谢!!!!!
- 怎么回事,我用在server2003上用NET2003写的程序在另一台2003server就不行了,汗!
- 构建动态树
- 撒分求助:Convert.ToDateTime 异常
- 谁有在线即时通信网页版的程序源代码
- 用C#编写一个简单的上位机串口
- c#中对comboBox或ListBox中内容的存储问题,急!!
- 有一道难题!!!
- webbrowse控件怎么使用
找了半天cmb的方法,它就说selectvalue取的就是valuemember的值。
真晕了!!!!
List<ListItem> mylist=new List<ListItem>();
ListItem item = new ListItem("我是值", "我是名字");
mylist.Add(item );
this.cmb.DataSource=mylist;
//使用DataSource
//调用 cmb.SelectedItem
{
public Form1()
{
InitializeComponent();
ListItem item = new ListItem("我是值", "我是名字");
this.cmb.Items.Add(item); } private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(((ListItem)cmb.SelectedItem).ID);
}
} public class ListItem
{
private string id = string.Empty;
private string name = string.Empty; public ListItem(string sid, string sname)
{
id = sid; name = sname;
} public override string ToString()
{
return this.name;
} public string ID
{
get { return this.id; }
set { this.id = value; }
} public string Name
{
get { return this.name; }
set { this.name = value; }
} }
在你的代码中只看到设置DisplayMember和ValueMember,没有看到数据绑定
SqlConnection con = new SqlConnection("Server=.;database=**;uid=sa;pwd=***");
con.Open();
DataSet ds = new DataSet();
ds.Clear();
string SelectCommand = "select * from [user] ";
SqlDataAdapter sda = new SqlDataAdapter(SelectCommand, con);
sda.Fill(ds,"user" );
if(ds!=null&&ds.Tables.Count!=0)
{
try
{
this.comboBox1.SelectedIndex = -1;
this.comboBox1.DisplayMember = "name";
this.comboBox1.ValueMember = "id";
this.comboBox1.DataSource = ds.Tables[0].DefaultView;
}
catch (Exception )
{
MessageBox.Show("初始化错误" + e.ToString());
} }
ListItem item = new ListItem("我是值", "我是名字");
this.cmb.Items.Add(item);
这样取值:
((ListItem)cmb.SelectedItem).ID
//cmb.SelectedItem 是你定义的 ListItem 类型实例,
ListItem myItem=cmb.SelectedItem as ListItem;
//myItem.ID;
//myItem.Name;
还没有仔细看呢,就是看了看C#语法,就要做东西了!以前一直搞java!
个人还是不太喜欢绑定数据库,数据层还是单独独立出去,设计DAO负责从数据库的操作,数据最后还是封装起来操作,这样系统更健壮些!
绑定到数据库耦合太大了,不太好啊。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
ComboBoxItem item = new ComboBoxItem("这是id", "这是vlaue");
this.comboBox1.Items.Add(item);
this.comboBox1.ValueMember = "Value";
this.comboBox1.DisplayMember = "ID";
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBoxItem item=this.comboBox1.SelectedItem as ComboBoxItem;
if (item==null)
return; MessageBox.Show(item.ID + " " + item.Value);
}
} public class ComboBoxItem
{
public ComboBoxItem(string ID, string Value)
{
this.ID = ID;
this.Value = Value;
} public string ID { get; set; }
public string Value { get; set; }
}
我是看了那个类的方法介绍,上面 cmb。selectedValue就是设置valueMember,如果用那个取是null。
你这种方法也不错。不过有时间还的研究研究 它 那个selectedValue!
一是用as操作符
设计一个抽象基类
abstract class BaseDTO
{
public abstract object DisplayMember();
public abstract object ValueMember();
}
每个子类实体类去实现那个是显示的,那个是隐藏的!
这样在取值的时候就通用了。
(BaseDTO)cmb.SelectedItem).ValueMember()
(BaseDTO)cmb.SelectedItem).DisplayMember()