用循环给combobox添加DisplayMember属性?

tulist 2010-01-26 11:00:18
只能给combobox绑定数据的字段设置DisplayMember和ValueMember的值吗?

ClientsBLL bll=new ClientsBLL();
List<Client> list=new List<Client>();
list = bll.GetClients();

for (int i = 0; i < list.Count; i++)
{
cboClient.Items.Add(list[i].行业类别.ToString());
cboClient.DisplayMember = list[i].行业类别.ToString();
cboClient.ValueMember = list[i].行业类别ID.ToString();
}

像上面这样用For循环设置combobox的DisplayMember和ValueMember属性出错了。
不知道combobox的这两个属性能否用循环来赋值?如果可以该怎么解决?
...全文
340 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunchen891229 2010-01-27
  • 打赏
  • 举报
回复
路过,学习了~
龙宜坡 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xray2005 的回复:]
你这样循环添加是不对的。

你可以这样:

ClientsBLL bll=new ClientsBLL();
List <Client> list=new List <Client>();
list = bll.GetClients();
for (int i = 0; i < list.Count; i++)
  {
        cboClient.Items.Add(list[i].行业类别.ToString(),list[i].行业类别ID.ToString());
    }


或者这样:
首先保证Client类里面有名称为:行业类别、行业类别ID的两个属性。然后直接绑定。
ClientsBLL bll=new ClientsBLL();
List <Client> list=new List <Client>();
list = bll.GetClients();
cboClient.DataSource=list;
cboClient.DisplayMember = "行业类别";
cboClient.ValueMember = "行业类别ID";


[/Quote]


好像要反过来写吧?
cboClient.DisplayMember = "行业类别";
cboClient.ValueMember = "行业类别ID";
cboClient.DataSource=list;
xray2005 2010-01-27
  • 打赏
  • 举报
回复
你这样循环添加是不对的。

你可以这样:

ClientsBLL bll=new ClientsBLL();
List <Client> list=new List <Client>();
list = bll.GetClients();
for (int i = 0; i < list.Count; i++)
{
cboClient.Items.Add(list[i].行业类别.ToString(),list[i].行业类别ID.ToString());
}


或者这样:
首先保证Client类里面有名称为:行业类别、行业类别ID的两个属性。然后直接绑定。
ClientsBLL bll=new ClientsBLL();
List <Client> list=new List <Client>();
list = bll.GetClients();
cboClient.DataSource=list;
cboClient.DisplayMember = "行业类别";
cboClient.ValueMember = "行业类别ID";

ILOVE_ASPNET 2010-01-27
  • 打赏
  • 举报
回复
高手好多哦,学习了。。
xray2005 2010-01-27
  • 打赏
  • 举报
回复
SORRY,WinfORM的COMBOXBOX的ITEMS.ADD方法是需要的参数是OBJ,没有2个参数。

那么。你就这样了:

ClientsBLL bll=new ClientsBLL();
List <Client> list=new List <Client>();
list = bll.GetClients();
cboClient.DataSource=list;
cboClient.DisplayMember = "行业类别";
cboClient.ValueMember = "行业类别ID";


PS:好像要反过来写吧? //和顺序无关,都可以
flyerwing 2010-01-27
  • 打赏
  • 举报
回复
class ListItem
{
private string key;
private string value;
.....
}
自己写这个好象成的吧
龙宜坡 2010-01-27
  • 打赏
  • 举报
回复
个人建议,别用汉字做变量名。
龙宜坡 2010-01-27
  • 打赏
  • 举报
回复

两种方法:
 public class ClientsBLL
{
private string m_行业类别;
private int m_行业Id;

public string 行业类别
{
get { return m_行业类别; }
set { m_行业类别 = value; }
}

public int 行业Id
{
get { return m_行业Id; }
set { m_行业Id = value; }
}
}

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

#region 随便组织点数据
ClientsBLL _bll0 = new ClientsBLL();
_bll0.行业Id = 0;
_bll0.行业类别 = "行业0";
ClientsBLL _bll1 = new ClientsBLL();
_bll1.行业Id = 1;
_bll1.行业类别 = "行业1";
ClientsBLL _bll2 = new ClientsBLL();
_bll2.行业Id = 2;
_bll2.行业类别 = "行业2";
#endregion

List<ClientsBLL> lst = new List<ClientsBLL>();
lst.Add(_bll0);
lst.Add(_bll1);
lst.Add(_bll2);

BindTest(lst);
}

//绑定
private void BindTest(List<ClientsBLL> Clients)
{
comboBox1.DisplayMember = "行业类别";//一定要和属性名称相符
comboBox1.ValueMember = "行业Id";
comboBox1.DataSource = Clients;
//comboBox1.DisplayMember = "行业类别";//一定要和属性名称相符,绑定前后都可以
//comboBox1.ValueMember = "行业Id";

}

//逐个添加的方法
private void AddTest(List<ClientsBLL> Clients)
{
comboBox1.DisplayMember = "行业类别";//一定要和属性名称相符
comboBox1.ValueMember = "行业Id";
for (int i = 0; i < Clients.Count; i++)
{
comboBox1.Items.Add(Clients[i]);
}
comboBox1.SelectedIndex = 0;

}
}
tulist 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xray2005 的回复:]
你这样循环添加是不对的。

你可以这样:

ClientsBLL bll=new ClientsBLL();
List <Client> list=new List <Client>();
list = bll.GetClients();
for (int i = 0; i < list.Count; i++)
  {
        cboClient.Items.Add(list[i].行业类别.ToString(),list[i].行业类别ID.ToString());
    }


或者这样:
首先保证Client类里面有名称为:行业类别、行业类别ID的两个属性。然后直接绑定。
ClientsBLL bll=new ClientsBLL();
List <Client> list=new List <Client>();
list = bll.GetClients();
cboClient.DataSource=list;
cboClient.DisplayMember = "行业类别";
cboClient.ValueMember = "行业类别ID";


[/Quote]

错误提示说:“Add”方法没有采用“2”个参数的重载。

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧