两个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].Defaul