积极答复者
dataGridView显示不了修改后的数据(vs2010)

问题
-
原本list里面已有一条数据,现在想修改成两条,但是绑定到dataGridView后只显示一条,最新的显示不了,我下断点后确定dataSource里面确实2条,但就是不显示。这是为什么,大家看看这代码有什么问题?
DataTable dtUser = new DataTable(); Common common = new Common(); List<EntityUnit> entityUnitList = new List<EntityUnit>(); public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { loadCom(); EntityUnit eu = new EntityUnit(); eu.ID = 1; eu.UnitName = "三毛"; entityUnitList.Add(eu); this.bindingSource1.DataSource = entityUnitList; this.dataGridView1.DataSource = bindingSource1; this.dataGridView1.Refresh(); } public void loadCom() { dtUser = common.GetDataTable("select * from G_User"); DataRow dr2 = dtUser.NewRow(); dr2[0] = 0; dr2[1] = "请选择"; dtUser.Rows.InsertAt(dr2, 0); this.comboBox1.DataSource = dtUser; this.comboBox1.DisplayMember = "UserName"; this.comboBox1.ValueMember = "ID"; this.comboBox1.SelectedIndex = 0; DataTable dtUser1 = dtUser.DefaultView.ToTable(); this.comboBox2.DataSource = dtUser1; this.comboBox2.DisplayMember = "UserName"; this.comboBox2.ValueMember = "ID"; this.comboBox2.SelectedIndex = 0; DataTable dtUser3 = dtUser.DefaultView.ToTable(); this.comboBox3.DataSource = dtUser3; this.comboBox3.DisplayMember = "UserName"; this.comboBox3.ValueMember = "ID"; this.comboBox3.SelectedIndex = 0; } public EntityUnit getDataByUserID(int userID) { EntityUnit en = new EntityUnit(); if (userID == 1) { en.ID = 11; en.UnitName = "张三"; } else { en.ID = 21; en.UnitName = "李四"; } return en; } private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { int id = comboBox1.SelectedValue.ToString() == "System.Data.DataRowView" ? 0 : int.Parse(comboBox1.SelectedValue.ToString()); if (id > 0) { full(id); } } private void comboBox2_SelectedValueChanged(object sender, EventArgs e) { int id = comboBox2.SelectedValue.ToString() == "System.Data.DataRowView" ? 0 : int.Parse(comboBox2.SelectedValue.ToString()); if (id > 0) { full(id); } } private void comboBox3_SelectedValueChanged(object sender, EventArgs e) { int id = comboBox3.SelectedValue.ToString() == "System.Data.DataRowView" ? 0 : int.Parse(comboBox3.SelectedValue.ToString()); if (id > 0) { full(id); } } public void full(int id) { EntityUnit aa = getDataByUserID(id); entityUnitList.Add(aa); this.bindingSource1.DataSource = entityUnitList; this.dataGridView1.DataSource = bindingSource1; this.dataGridView1.Refresh(); } }
答案
-
尝试这样修改代码:
EntityUnit aa = getDataByUserID(id);
entityUnitList.Add(aa);
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = entityUnitList;
this.dataGridView1.Refresh();
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 Mike Dos ZhangModerator 2011年6月23日 4:47
全部回复
-
尝试这样修改代码:
EntityUnit aa = getDataByUserID(id);
entityUnitList.Add(aa);
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = entityUnitList;
this.dataGridView1.Refresh();
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 Mike Dos ZhangModerator 2011年6月23日 4:47