积极答复者
朋友们,请教一下,如果把对象列表绑定到datagridview啊

问题
-
朋友们,现在有对象,会员,我从表中取出了所有会员对象,但如何绑定到datagridview上显示啊,本来用datatable直接绑了,可这对象,如何是好?求助
C#初学者,所以提的问题对各位前辈朋友来说可能很简单,也可能问法也不对,非常期待大家的回答……- 已移动 Sheng Jiang 蒋晟Moderator 2011年8月1日 20:02 System.Windows.Forms (发件人:Visual C#)
答案
-
朋友们,现在有对象,会员,我从表中取出了所有会员对象,但如何绑定到datagridview上显示啊,本来用datatable直接绑了,可这对象,如何是好?求助
C#初学者,所以提的问题对各位前辈朋友来说可能很简单,也可能问法也不对,非常期待大家的回答……
你好:)假设你有一个对象Member,且其公开属性都是基本类型(非符合类型,否则必须要重写符合类型的ToString方法,否则输出是Namespace.ClassName)。
那么绑定方法:
List<Member> members = new List<Member>();
//使用for从数据库读出并且加到List列表中,此处省略。
DataGridView.DataSource = members;
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 永远的小六 2011年7月27日 23:13
-
public partial class Form15 : Form { string str="server=.;uid=sa;pwd=sa;database=student"; public Form15() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Area ",con); SqlDataAdapter adp = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adp.Fill(ds); //定义泛型集合 IList<Area> areas = new List<Area>(); //把datatable中的数据放入到泛型集合中 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Area aa = new Area(); aa.area = ds.Tables[0].Rows[i]["area"].ToString(); aa.father = ds.Tables[0].Rows[i]["father"].ToString(); areas.Add(aa); } //绑定泛型集合 this.dataGridView1.DataSource = areas; } } /// <summary> /// 数据库表对应的实体类 /// </summary> public class Area { public string area { get; set; } public string father { get; set; } }
http://blog.csdn.net/zx13525079024- 已标记为答案 永远的小六 2011年7月27日 23:13
全部回复
-
朋友们,现在有对象,会员,我从表中取出了所有会员对象,但如何绑定到datagridview上显示啊,本来用datatable直接绑了,可这对象,如何是好?求助
C#初学者,所以提的问题对各位前辈朋友来说可能很简单,也可能问法也不对,非常期待大家的回答……
你好:)假设你有一个对象Member,且其公开属性都是基本类型(非符合类型,否则必须要重写符合类型的ToString方法,否则输出是Namespace.ClassName)。
那么绑定方法:
List<Member> members = new List<Member>();
//使用for从数据库读出并且加到List列表中,此处省略。
DataGridView.DataSource = members;
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 永远的小六 2011年7月27日 23:13
-
public partial class Form15 : Form { string str="server=.;uid=sa;pwd=sa;database=student"; public Form15() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Area ",con); SqlDataAdapter adp = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adp.Fill(ds); //定义泛型集合 IList<Area> areas = new List<Area>(); //把datatable中的数据放入到泛型集合中 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Area aa = new Area(); aa.area = ds.Tables[0].Rows[i]["area"].ToString(); aa.father = ds.Tables[0].Rows[i]["father"].ToString(); areas.Add(aa); } //绑定泛型集合 this.dataGridView1.DataSource = areas; } } /// <summary> /// 数据库表对应的实体类 /// </summary> public class Area { public string area { get; set; } public string father { get; set; } }
http://blog.csdn.net/zx13525079024- 已标记为答案 永远的小六 2011年7月27日 23:13