简体   繁体   中英

How to select multiple rows in 2 DataGridViews

How to select multiple rows in 2 datagridviews like on screenshot? Example For 1 row I did :

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

But don't know how to do with a multiple rows.

To synchronize selected rows of two DataGridView , you can handle SelectionChanged event of first grid and set selected rows of second grid this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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