如何在2个DataGridViews中选择多行

时间:2016-06-11 15:23:57

标签: c# winforms datagridview

如何在屏幕截图中的2个datagridviews中选择多行? Example 我做了一行:

private void dataDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    Selection();
}   
private void Selection()
{
    table2DataGridView.ClearSelection();
    int selected = Convert.ToInt32(table1DataGridView.CurrentRow.Index);
    if (table1DataGridView.Rows.Count != 0)
    {
        table2DataGridView.Rows[selected].Selected = true;
    }
}

但不知道如何处理多行。

1 个答案:

答案 0 :(得分:0)

要同步两个DataGridView的选定行,您可以处理第一个网格的SelectionChanged事件并以这种方式设置第二个网格的选定行:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    this.dataGridView2.ClearSelection();
    this.dataGridView1.SelectedRows.Cast<DataGridViewRow>().Select(x => x.Index)
        .ToList().ForEach(i =>
        {
            if (i < this.dataGridView2.RowCount)
                this.dataGridView2.Rows[i].Selected = true;
        });
}