Disable Cell Highlighting in a datagridview

前端 未结 10 1880
后悔当初
后悔当初 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:12
    Private Sub DGW2_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DGW2.DataBindingComplete
        Dim mygrid As DataGridView
        mygrid = CType(sender, DataGridView)
        mygrid.ClearSelection()
    End Sub
    
    0 讨论(0)
  • 2020-12-08 14:13

    The quickest way to do this to handle cells with different colours, without needing to refire any events, would be to do something like this:

    private void dgvMyControl_SelectionChanged(object sender, EventArgs e)
    {
        dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.SelectionBackColor = dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.BackColor
    
    }
    
    You will need to put in an iterator if you allow multiple selections

    (EDIT)

    actually, this needs to be done at time for data population. it doesn't appear to work in the on selection changed method. So after populating the data into the table, you need to iterate through the cells and change their selected background to match their normal background. Something like this (syntax may be a little off, I'm converting it from my vb code):

    foreach (datarow r in dgv.rows)
    {
      foreach (datacell c in r.cells)
      {
         c.Style.SelectionBackColor = c.Style.BackColor
      }
    }
    
    0 讨论(0)
  • 2020-12-08 14:19

    Messing around and this also works, as i only want to change the cell background colour in the 2nd column when a cell is clicked:

            Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    
        Dim row As Integer = DataGridView1.CurrentCellAddress.Y
        Dim column As Integer = DataGridView1.CurrentCellAddress.X
    
        If column = 1 Then
            Me.DataGridView1.CurrentCell.Selected = False
            DataGridView1.Item(column, row).Style.BackColor = SelectColour()
        End If
    
    End Sub
    
    0 讨论(0)
  • 2020-12-08 14:24
    <DataGrid ItemsSource="{Binding Credits}" x:Name="Grid"
                              HorizontalAlignment="Left" RowBackground="Transparent">
    
    0 讨论(0)
提交回复
热议问题