List<string> strSource = new List<string>();
strSource.Add("one");
strSource.Add("two");
strSource.Add("three");
dataGridView1.DataSource = strSource;
结果DataGridView中显示的为什么是字符串的长度呢?
datagridview 绑定List<string>
答案:3 悬赏:20 手机版
解决时间 2021-03-19 18:10
- 提问者网友:浪荡绅士
- 2021-03-18 23:25
最佳答案
- 五星知识达人网友:十鸦
- 2021-03-19 00:33
因为你没有指定DataPropertyName这个绑定字段的属性。你可以先定义一个类,例如:
public class Person
{
public Person(string _name)
{
this.name = _name;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
然后在你的DataGridView的DataPropertyName绑定name.
接下来:public List<Person> list = new List<Person>();
list.Add(new Person("张三"));
list.Add(new Person("李四"));
list.Add(new Person("王五"));
this.dataGridView1.DataSource = list;
这样就行了
public class Person
{
public Person(string _name)
{
this.name = _name;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
然后在你的DataGridView的DataPropertyName绑定name.
接下来:public List<Person> list = new List<Person>();
list.Add(new Person("张三"));
list.Add(new Person("李四"));
list.Add(new Person("王五"));
this.dataGridView1.DataSource = list;
这样就行了
全部回答
- 1楼网友:夜风逐马
- 2021-03-19 02:38
IList<String> list_string= new List<String>();
DataGridView.DataSource = list_string.Select(x => new { Value = x }).ToList();
- 2楼网友:北方的南先生
- 2021-03-19 01:02
list listname = new list();
改成
ilist listname = new list();
这是我的你参考下
public static ilist getstuinfo()
{
ilist stuinfos = new list();
string sql = "select * from stuinfo";
using (datatable dt = dbhelper.getdataset(sql))
{
foreach (datarow row in dt.rows)
{
stuinfo stu = new stuinfo();
stu.stuno=(int)row["stuno"];
stu.stuname=(string)row["stuname"];
stu.stuage = (int)row["stuage"];
stu.stuseet = (int)row["stuseet"];
stu.stusex = (string)row["stusex"];
stu.stuadress = (string)row["stuadress"];
stuinfos.add(stu);
}
}
return stuinfos;
}
gridview1.datasource = bll.stuinfomanager.getstuinfo();
gridview1.databind();
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯