怎样动态循环给list赋值呢

lsp69 2015-12-09 12:52:30
有以下代码 :
public partial class Form1 : Form
{
public class ComboBoxMember
{

public string Id
{
get;
set;
}

public string Text
{
get;
set;
}

}
List<ComboBoxMember> list = new List<ComboBoxMember>();
public Form1()
{
InitializeComponent();

list = new List<ComboBoxMember>() {
new ComboBoxMember(){ Id="0", Text="张三a"},
new ComboBoxMember(){ Id="1", Text="李四abc"},
new ComboBoxMember(){ Id="2", Text="张三三adbx"},
new ComboBoxMember(){ Id="3", Text="李四四"},
new ComboBoxMember(){ Id="4", Text="张张三"},
new ComboBoxMember(){ Id="5", Text="李李四"}
};
comboBox1.Items.AddRange(list.ToArray());
comboBox1.DisplayMember = "Text";
comboBox1.ValueMember = "Id";

}
}

其中有这里面的数据,我是从数据库直接获取到的DataTable列表,怎样放入到list里呢?
以下目前是写死固定的。
list = new List<ComboBoxMember>() {
new ComboBoxMember(){ Id="0", Text="张三a"},
new ComboBoxMember(){ Id="1", Text="李四abc"},
new ComboBoxMember(){ Id="2", Text="张三三adbx"},
new ComboBoxMember(){ Id="3", Text="李四四"},
new ComboBoxMember(){ Id="4", Text="张张三"},
new ComboBoxMember(){ Id="5", Text="李李四"}
};



...全文
1034 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
浩方软件HFWMS 2015-12-09
  • 打赏
  • 举报
回复
引用 楼主 lsp69 的回复:
有以下代码 : public partial class Form1 : Form { public class ComboBoxMember { public string Id { get; set; } public string Text { get; set; } } List<ComboBoxMember> list = new List<ComboBoxMember>(); public Form1() { InitializeComponent(); list = new List<ComboBoxMember>() { new ComboBoxMember(){ Id="0", Text="张三a"}, new ComboBoxMember(){ Id="1", Text="李四abc"}, new ComboBoxMember(){ Id="2", Text="张三三adbx"}, new ComboBoxMember(){ Id="3", Text="李四四"}, new ComboBoxMember(){ Id="4", Text="张张三"}, new ComboBoxMember(){ Id="5", Text="李李四"} }; comboBox1.Items.AddRange(list.ToArray()); comboBox1.DisplayMember = "Text"; comboBox1.ValueMember = "Id"; } } 其中有这里面的数据,我是从数据库直接获取到的DataTable列表,怎样放入到list里呢? 以下目前是写死固定的。 list = new List<ComboBoxMember>() { new ComboBoxMember(){ Id="0", Text="张三a"}, new ComboBoxMember(){ Id="1", Text="李四abc"}, new ComboBoxMember(){ Id="2", Text="张三三adbx"}, new ComboBoxMember(){ Id="3", Text="李四四"}, new ComboBoxMember(){ Id="4", Text="张张三"}, new ComboBoxMember(){ Id="5", Text="李李四"} };
引用 1 楼 xdashewan 的回复:
1.改写数据库检索层,直接赋值ComboBoxMember替换table 2.把table里的数据循环赋值ComboBoxMember
不知道怎样赋值到对象数组里,把代码写一下吧
  • 打赏
  • 举报
回复
直接赋值为DataTable,属性设置分别为列名称即可。 或者http://www.cnblogs.com/sydeveloper/archive/2013/04/26/3044369.html
xdashewan 2015-12-09
  • 打赏
  • 举报
回复
1.改写数据库检索层,直接赋值ComboBoxMember替换table 2.把table里的数据循环赋值ComboBoxMember
BitCoffee 2015-12-09
  • 打赏
  • 举报
回复

//comboBox可以直接绑定datatable
comboBox1.DataSource = datatable;
comboBox1.DisplayMember = "Text";
comboBox1.ValueMember = "Id";
正怒月神 2015-12-09
  • 打赏
  • 举报
回复

list = new List<ComboBoxMember>() ;
Foreach(DataRow item in dt.Rows)
{
ComboBoxMember c = new ComboBoxMember(){ Id=item["Id"], Text=item["Text"]};
list.add(c);
}

111,094

社区成员

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

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

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