两个dataGridView做主从表数据联动效果,在主表的什么事件中写代码?

ltolll 2014-01-17 03:12:15
主表dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
目前是在主表SelectionChanged事件中写代码。

private void dgvMaster_SelectionChanged(object sender, EventArgs e)
{
if (null != dgvMaster.CurrentRow)
{
string icId = dgvMaster.Rows[dgvMaster.CurrentRow.Index].Cells["ICId"].Value.ToString();
dgvSlave.DataSource = 取子表数据(icId);
}
else
dgvSlave.DataSource = null;
}

这样做的问题是,当dgvMaster中已经存在数据时,如果dgvMaster重新绑定数据,则SelectionChanged事件要发生2次(同样的取数据动作),这造成了1次无用的取数据动作,如何避免?
...全文
302 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ltolll 2014-01-24
  • 打赏
  • 举报
回复
自己找到了解决方法: http://blog.csdn.net/ltolll/article/details/18599155
ltolll 2014-01-21
  • 打赏
  • 举报
回复
引用 7 楼 u011130289 的回复:
这样肯定是你的判断代码有问题了,断点看看为什么绑定2次
并不是绑定了2次(绑定了1次),而是每次绑定时dataGridView.SelectionChanged事件发生了2次。 经测试发现,SelectionChanged事件发生次数与触发方式和dataGridView中是否有数据有关。 1.由数据绑定触发,如果dataGridView中原来无数据,则SelectionChanged事件发生1次; 2.由数据绑定触发,如果dataGridView中原来有数据,则SelectionChanged事件发生2次; 3.由用户(鼠标或键盘动作)触发,dataGridView中原来有数据,则SelectionChanged事件发生1次;
ltolll 2014-01-19
  • 打赏
  • 举报
回复
引用 4 楼 jianchun_liu 的回复:
可以建主副表关系的,在主表中建建外键连接到副表的主键上, 关系在第一次加载数据时建好,后面不用写任务代码,就可实现联动了.
您说的是不是微软提供的DataGridView主从表显示示例? http://www.cnblogs.com/NETCCB/articles/1335347.html 这种通过BindingSource等控件实现的方法我用过,不知是不是我没搞清楚,它有个我不能接受的缺点: 对于主表必须把整个表的数据加载到数据集(实际情况是,我每次只需要查一小部分数据)
ltolll 2014-01-19
  • 打赏
  • 举报
回复
引用 3 楼 u011130289 的回复:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { cd.sqlcon.Open(); string sqlstrProvince = "select province from lqj_province"; SqlDataAdapter sdaProvince = new SqlDataAdapter(sqlstrProvince,cd.sqlcon); DataSet Provinceds = new DataSet(); sdaProvince.Fill(Provinceds); lqj_ProvinceDropDownList.DataSource = Provinceds.Tables[0].DefaultView; lqj_ProvinceDropDownList.DataValueField = "Province"; lqj_ProvinceDropDownList.DataBind(); string sqlstrCity = "select city from lqj_city where lqj_city.father=(select provinceID from lqj_province where province='" + lqj_ProvinceDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaCity = new SqlDataAdapter(sqlstrCity, cd.sqlcon); DataSet dsCity = new DataSet(); sdaCity.Fill(dsCity); lqj_CityDropDownList.DataSource = dsCity.Tables[0].DefaultView; lqj_CityDropDownList.DataValueField = "City"; lqj_CityDropDownList.DataBind(); string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } } protected void lqj_ProvinceDropDownList_SelectedIndexChanged(object sender, EventArgs e) { string sqlstrCity = "select city from lqj_city where lqj_city.father=(select provinceID from lqj_province where province='" + lqj_ProvinceDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaCity = new SqlDataAdapter(sqlstrCity, cd.sqlcon); DataSet dsCity = new DataSet(); cd.sqlcon.Open(); sdaCity.Fill(dsCity); lqj_CityDropDownList.DataSource = dsCity; lqj_CityDropDownList.DataValueField = "City"; lqj_CityDropDownList.DataBind(); string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } protected void lqj_CityDropDownList_SelectedIndexChanged(object sender, EventArgs e) { string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); cd.sqlcon.Open(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } } 三级联动的代码,你自己参考,改下,这是以前自己写的省市县的三级联动
谢谢回答,但问题的关键,不是实现不了联动,而是数据绑定时dataGridView.SelectionChanged事件发生了2次。上面的代码中没有涉及“dataGridView”控件的问题。
Regan-lin 2014-01-19
  • 打赏
  • 举报
回复
引用 5 楼 ltolll 的回复:
[quote=引用 3 楼 u011130289 的回复:] protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { cd.sqlcon.Open(); string sqlstrProvince = "select province from lqj_province"; SqlDataAdapter sdaProvince = new SqlDataAdapter(sqlstrProvince,cd.sqlcon); DataSet Provinceds = new DataSet(); sdaProvince.Fill(Provinceds); lqj_ProvinceDropDownList.DataSource = Provinceds.Tables[0].DefaultView; lqj_ProvinceDropDownList.DataValueField = "Province"; lqj_ProvinceDropDownList.DataBind(); string sqlstrCity = "select city from lqj_city where lqj_city.father=(select provinceID from lqj_province where province='" + lqj_ProvinceDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaCity = new SqlDataAdapter(sqlstrCity, cd.sqlcon); DataSet dsCity = new DataSet(); sdaCity.Fill(dsCity); lqj_CityDropDownList.DataSource = dsCity.Tables[0].DefaultView; lqj_CityDropDownList.DataValueField = "City"; lqj_CityDropDownList.DataBind(); string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } } protected void lqj_ProvinceDropDownList_SelectedIndexChanged(object sender, EventArgs e) { string sqlstrCity = "select city from lqj_city where lqj_city.father=(select provinceID from lqj_province where province='" + lqj_ProvinceDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaCity = new SqlDataAdapter(sqlstrCity, cd.sqlcon); DataSet dsCity = new DataSet(); cd.sqlcon.Open(); sdaCity.Fill(dsCity); lqj_CityDropDownList.DataSource = dsCity; lqj_CityDropDownList.DataValueField = "City"; lqj_CityDropDownList.DataBind(); string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } protected void lqj_CityDropDownList_SelectedIndexChanged(object sender, EventArgs e) { string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); cd.sqlcon.Open(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } } 三级联动的代码,你自己参考,改下,这是以前自己写的省市县的三级联动
谢谢回答,但问题的关键,不是实现不了联动,而是数据绑定时dataGridView.SelectionChanged事件发生了2次。上面的代码中没有涉及“dataGridView”控件的问题。[/quote] 这样肯定是你的判断代码有问题了,断点看看为什么绑定2次
Regan-lin 2014-01-18
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { cd.sqlcon.Open(); string sqlstrProvince = "select province from lqj_province"; SqlDataAdapter sdaProvince = new SqlDataAdapter(sqlstrProvince,cd.sqlcon); DataSet Provinceds = new DataSet(); sdaProvince.Fill(Provinceds); lqj_ProvinceDropDownList.DataSource = Provinceds.Tables[0].DefaultView; lqj_ProvinceDropDownList.DataValueField = "Province"; lqj_ProvinceDropDownList.DataBind(); string sqlstrCity = "select city from lqj_city where lqj_city.father=(select provinceID from lqj_province where province='" + lqj_ProvinceDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaCity = new SqlDataAdapter(sqlstrCity, cd.sqlcon); DataSet dsCity = new DataSet(); sdaCity.Fill(dsCity); lqj_CityDropDownList.DataSource = dsCity.Tables[0].DefaultView; lqj_CityDropDownList.DataValueField = "City"; lqj_CityDropDownList.DataBind(); string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } } protected void lqj_ProvinceDropDownList_SelectedIndexChanged(object sender, EventArgs e) { string sqlstrCity = "select city from lqj_city where lqj_city.father=(select provinceID from lqj_province where province='" + lqj_ProvinceDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaCity = new SqlDataAdapter(sqlstrCity, cd.sqlcon); DataSet dsCity = new DataSet(); cd.sqlcon.Open(); sdaCity.Fill(dsCity); lqj_CityDropDownList.DataSource = dsCity; lqj_CityDropDownList.DataValueField = "City"; lqj_CityDropDownList.DataBind(); string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } protected void lqj_CityDropDownList_SelectedIndexChanged(object sender, EventArgs e) { string sqlstrArea = "select area from lqj_area where lqj_area.father=(select cityID from lqj_city where city='" + lqj_CityDropDownList.SelectedItem.Text + "')"; SqlDataAdapter sdaArea = new SqlDataAdapter(sqlstrArea, cd.sqlcon); DataSet dsArea = new DataSet(); cd.sqlcon.Open(); sdaArea.Fill(dsArea); lqj_AreaDropDownList.DataSource = dsArea; lqj_AreaDropDownList.DataValueField = "Area"; lqj_AreaDropDownList.DataBind(); cd.sqlcon.Close(); } } 三级联动的代码,你自己参考,改下,这是以前自己写的省市县的三级联动
ltolll 2014-01-18
  • 打赏
  • 举报
回复
引用 1 楼 a13051335368 的回复:
可以让这个事件只走一次、、 判断第一次走了过后 下一次 直接跳过、 if判断
可是我数据绑定后当行切换的时候还需要事件发生。
jianchun_ 2014-01-18
  • 打赏
  • 举报
回复
可以建主副表关系的,在主表中建建外键连接到副表的主键上, 关系在第一次加载数据时建好,后面不用写任务代码,就可实现联动了.
枫c_2012 2014-01-17
  • 打赏
  • 举报
回复
可以让这个事件只走一次、、 判断第一次走了过后 下一次 直接跳过、 if判断

110,538

社区成员

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

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

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