2017-08-08 29 views
0

我有以下DataGridView前两列是DataGridViewImageCell允许用户点击DataGridViewImageCell但不改变现有的行选择

enter image description here

什么我不知道是我希望用户能够点击第一个单元格(带加号),并通过它的点击方式运行,但不会改变当前选择的行。

我不希望发生任何事情,如果点击第二列,没有点击事件没有选择更改。

目前我刚刚通过CellContentClick得到了点击事件。

private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    try 
    { 
     DataGridViewEx dgvGeometryAudit = (DataGridViewEx)sender; 
     //Test for first column name IMAGE_TOGGLE 
     if (dgvGeometryAudit.Columns[e.ColumnIndex].Name.Equals("IMAGE_TOGGLE", StringComparison.OrdinalIgnoreCase)) 
     { 
      ASSET_HEADER ah = (ASSET_HEADER)dgvGeometryAudit.Rows[e.RowIndex].DataBoundItem; 

      ExpandRow(dgvGeometryAudit, ah, e.RowIndex); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Exception: " + ex.Message); 
     this.Close(); 
    } 
} 

有没有办法做到这一点?

+0

我不知道是否有内置的方法或一些快速的方式,但如果你没有找到任何事情来到我的心灵do是将选中的所有行存储在列表中(索引),并且如果用户单击第一列或第二列,则只需执行该操作,取消全选,然后选择列表中的内容 –

+0

这不是最优雅的,但这是一个解决方案!将它贴在答案中@AleksaRistic – Hank

+0

如果有人回答其他问题,请稍等 –

回答

1

创建全局变量private List<int> selectedRows = new List<int>()然后里面cellClick事件中使用这样的:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex != 0 && e.ColumnIndex != 1) 
    { 
     if (dataGridView1.Rows[e.RowIndex].Selected == false) 
     { 
      dataGridView1.Rows[e.RowIndex].Selected = true; 
      selectedRows.Add(e.RowIndex); 
     } 
     else 
     { 
      dataGridView1.Rows[e.RowIndex].Selected = false; 
      selectedRows.Remove(e.RowIndex); 
     } 
    } 
    else 
    { 
     dataGridView1.ClearSelection(); 
     //Do your job here for that column/row 
     foreach(int r in selectedRows) 
     { 
      dataGridView1.Rows[r].Selected = true; 
     } 
    } 
} 
0

这里是将防止不必要的选择,以在第一时间发生的解决方案。你可能想改变它默认为您的正常选择模式..:

private void dataGridView1_CellMouseMove(object sender, 
             DataGridViewCellMouseEventArgs e) 
{ 
    dataGridView1.SelectionMode = e.ColumnIndex == yourImageColumIndex ? 
        DataGridViewSelectionMode.RowHeaderSelect : 
        DataGridViewSelectionMode.FullRowSelect; // or whatever you want 
}