Disable Cell Highlighting in a datagridview

前端 未结 10 1933
后悔当初
后悔当初 2020-12-08 13:30

How to disable Cell Highlighting in a datagridview, Highlighting should not happen even if I click on the cell.

Any thoughts please

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 14:06

    Did a quick websearch to find out how to make a datagridview selection non-selectable & got this (web page) hit.

    Calling ClearSelection on SelectionChanged can and does cause a double firing of the SelectionChanged event, at minimum.

    The first event is when the cell/row is selected and, of course, the SelectionChanged event is fired. The second firing is when ClearSelection is called as it causes (and logically so!) the selection of the datagridview to (again) changed (to no selection), thus firing SelectionChanged.

    If you have more code than simply ClearSelection going on, as such I do, you'll want to suppress this event until after your code is done. Here's an example:

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

提交回复
热议问题