子窗体
#region 增加地点 private void button_AddHosAdr_Click(object sender, EventArgs e) { DlgAddPeBatchHspAdr dlgpbha = new DlgAddPeBatchHspAdr(); dlgpbha.EvtRefresh += dlgpbha_EvtRefresh; //显示对话框 dlgpbha.ShowDialog(); } void dlgpbha_EvtRefresh(object sender, RefreshEventArgs e) { try { if (sender != null) { List<pe_srvhosp> items = sender as List<pe_srvhosp>;//新增数据 if (items != null && items.Count > 0) { //原有数据 BindingCollection<pe_srvhosp> list = this.dataGridView_pe_srvhosp_list.DataSource as BindingCollection<pe_srvhosp>; if (list != null && list.Count > 0) { List<pe_srvhosp> listsrvhosp = list.Intersect(items, new pe_srvhosp_Equality()).ToList();//重复数据 if (listsrvhosp != null && listsrvhosp.Count > 0) { var chaji = items.Except(listsrvhosp, new pe_srvhosp_Equality()).ToList();//差集(A集合有,B没有) foreach (pe_srvhosp srv in chaji) { list.Add(srv);//新增数据去重后添加到原有数据 } this.dataGridView_pe_srvhosp_list.DataSource = new BindingCollection<pe_srvhosp>(list); this.dataGridView_pe_srvhosp_list.Refresh(); } else { foreach (pe_srvhosp srv in items) { list.Add(srv); } this.dataGridView_pe_srvhosp_list.DataSource = list; this.dataGridView_pe_srvhosp_list.Refresh(); int hosp_id = Convert.ToInt32(dataGridView_pe_srvhosp_list.Rows[0].Cells["Column_hosp_id"].Value); //获取第一行中列名为Column_hosp_id的值 if (hosp_id <= 0) { dataGridView_pe_srvhosp_list.Rows.RemoveAt(0); //移除第一行数据 } } } } } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "体检批次", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion
主窗体
DlgAddPeBatchHspAdr窗口 //在最上方声明变量 public event EventHandler<RefreshEventArgs> EvtRefresh; #region 保存体检地点 private void button_save_Click(object sender, EventArgs e) { try { if (dataGridView_main.SelectedRows != null && dataGridView_main.SelectedRows.Count > 0) { pe_srvhosp srvhosp = null; List<pe_srvhosp> list = new List<pe_srvhosp>(); foreach (DataGridViewRow row in dataGridView_main.SelectedRows) { srvhosp = row.DataBoundItem as pe_srvhosp; list.Add(srvhosp); } if (EvtRefresh != null) { EvtRefresh(list, null); } this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "增加医院", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion