禁用数据网格视图中的单元格突出显示

Ram*_*mji 42 .net vb.net datagridview winforms

如何在数据网格视图中禁用单元格突出显示,即使单击单元格也不应突出显示.

请问任何想法

Der*_*k H 98

ForeColor/BackColor kludge不适合我,因为我有不同颜色的细胞.因此,对于同一地点的任何人,我发现了一种更类似于实际禁用该功能的解决方案.

设置SelectionChanged事件以调用运行的方法ClearSelection

private void datagridview_SelectionChanged(object sender, EventArgs e)
{
    this.datagridview.ClearSelection();
}
Run Code Online (Sandbox Code Playgroud)


jhe*_*ngs 63

我发现为"禁用",突出的唯一方法是设置SelectionBackColorSelectionForeColorDefaultCellStyle相同的作为BackColorForeColor分别.您可以在表单的Load事件上以编程方式执行此操作,但我也在设计器中完成了此操作.

像这样的东西:

Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Me.DataGridView1.DefaultCellStyle.BackColor
Me.DataGridView1.DefaultCellStyle.SelectionForeColor = Me.DataGridView1.DefaultCellStyle.ForeColor
Run Code Online (Sandbox Code Playgroud)

  • 如果某些单元格的颜色不同,则默认为背景颜色,这不起作用. (3认同)
  • 如果循环遍历行,设置颜色,还可以在同一循环中设置选择颜色.然后它工作得很好.更一般地说,您可以在设置正常颜色时设置选择颜色. (3认同)

小智 5

快速进行了一次网络搜索,以查找如何使datagridview选择变为不可选择,并使其获得成功。

至少在SelectionChanged上调用ClearSelection可以而且确实会导致两次触发SelectionChanged事件。

第一个事件是选择单元格/行时,当然会触发SelectionChanged事件。第二次触发是在调用ClearSelection时,因为它导致(逻辑上如此!)对datagridview的选择(再次)更改(变为无选择),从而触发SelectionChanged。

如果您执行的代码不只是ClearSelection所需要的代码(例如我这样做),您将希望取消此事件,直到代码完成之后。这是一个例子:

 private void dgvMyControl_SelectionChanged(object sender, EventArgs e)
{
  //suppresss the SelectionChanged event
  this.dgvMyControl.SelectionChanged -= dgvMyControl_SelectionChanged;

  //grab the selectedIndex, if needed, for use in your custom code
  // do your custom code here

  // finally, clear the selection & resume (reenable) the SelectionChanged event 
  this.dgvMyControl.ClearSelection();
  this.dgvMyControl.SelectionChanged += dgvMyControl_SelectionChanged;
}
Run Code Online (Sandbox Code Playgroud)