两个dataGridView做主从表数据联动效果,在主表的什么事件中写代码?
主表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次无用的取数据动作,如何避免?
[解决办法]
可以让这个事件只走一次、、
判断第一次走了过后 下一次 直接跳过、 if判断
[解决办法]
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();
}
}
三级联动的代码,你自己参考,改下,这是以前自己写的省市县的三级联动
[解决办法]
可以建主副表关系的,在主表中建建外键连接到副表的主键上,
关系在第一次加载数据时建好,后面不用写任务代码,就可实现联动了.
[解决办法]