繁体   English   中英

如何在2个DataGridViews中选择多行

[英]How to select multiple rows in 2 DataGridViews

如何选择2个datagridviews中的多行,如截图? 示例我做了1行:

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;
    }
}

但不知道如何处理多行。

要同步两个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;
        });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM