【问题标题】:How to select multiple rows in 2 DataGridViews如何在 2 个 DataGridViews 中选择多行
【发布时间】:2016-06-12 03:42:07
【问题描述】:

如何在 2 个数据网格视图中选择多行,如屏幕截图所示? Example 我做了 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;
    }
}

但不知道如何处理多行。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 2013-04-04
      • 1970-01-01
      • 2013-06-21
      相关资源
      最近更新 更多