SelectedRows相同的两个datagridview

时间:2011-04-14 22:16:34

标签: c# winforms datagridview

我有一个包含两个标签的tabcontrol。

每个都有一个datagridview。

它们具有相同的绑定源。

我在每个上都有同步选择的问题。

现在,我有:

private void dgvGeoObjectsSecondView_SelectionChanged(object sender, EventArgs e)
{
   if (tabControl1.SelectedIndex == 1)
   {
       foreach (DataGridViewRow dvRow in this.dgvGeoObjectsSecondView.Rows)
       {
           foreach (DataGridViewRow dvRowFirstView in this.dgvGeoObjectsFirstView.Rows)
           {
               if ((long) ((DataRowView) dvRow.DataBoundItem)["ObiektID"] ==
                  (long) ((DataRowView) dvRowFirstView.DataBoundItem)["ObiektID"])
                        dvRowFirstView.Selected = dvRow.Selected;
            }
       }
   }
}

private void dgvGeoObjectsFirstView_SelectionChanged(object sender, EventArgs e)
{
    if (tabControl1.SelectedIndex==0)
    {
        foreach (DataGridViewRow dvRow in this.dgvGeoObjectsFirstView.Rows)
        {
            foreach (DataGridViewRow dvRowSecondView in this.dgvGeoObjectsSecondView.Rows)
            {
                if ((long)((DataRowView)dvRow.DataBoundItem)["ObiektID"] ==
                   (long)((DataRowView)dvRowSecondView.DataBoundItem)["ObiektID"])
                         dvRowSecondView.Selected = dvRow.Selected;
            }
        }
    }
}

但是,当我更改tabpage时,第二个网格中的选区正在清除。

有任何想法或遇到此问题的经历?

1 个答案:

答案 0 :(得分:0)

这可能是由于绑定在控件首次可见之前无效。解决方法是使第二个网格暂时可见。