[英]Disable edits on datagridview but still allow for highlighting to copy and paste cells
[英]Disable Cell Highlighting in a datagridview
如何在 datagridview 中禁用單元格突出顯示,即使我單擊單元格也不應該發生突出顯示。
任何想法請
ForeColor/BackColor kludge 對我不起作用,因為我有不同顏色的單元格。 因此,對於同一地點的任何人,我找到了一個更類似於實際禁用該能力的解決方案。
設置SelectionChanged
事件以調用運行ClearSelection
的方法
private void datagridview_SelectionChanged(object sender, EventArgs e)
{
this.datagridview.ClearSelection();
}
我發現“禁用”突出顯示的唯一方法是將DefaultCellStyle
的SelectionBackColor
和SelectionForeColor
分別設置為與BackColor
和ForeColor
相同。 您可能可以在窗體的Load
事件上以編程方式執行此操作,但我也在設計器中完成了此操作。
像這樣的東西:
Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Me.DataGridView1.DefaultCellStyle.BackColor
Me.DataGridView1.DefaultCellStyle.SelectionForeColor = Me.DataGridView1.DefaultCellStyle.ForeColor
做了一個快速的網絡搜索,以找出如何使 datagridview 選擇不可選擇並得到這個(網頁)點擊。
在 SelectionChanged 上調用 ClearSelection 可以並且確實導致 SelectionChanged 事件的雙重觸發,至少。
第一個事件是選擇單元格/行時,當然,會觸發 SelectionChanged 事件。 第二次觸發是在調用 ClearSelection 時,因為它導致(邏輯上如此!)數據網格視圖的選擇(再次)更改(無選擇),從而觸發 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;
}
處理具有不同顏色的單元格而不需要重新觸發任何事件的最快方法是執行以下操作:
private void dgvMyControl_SelectionChanged(object sender, EventArgs e)
{
dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.SelectionBackColor = dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.BackColor
}
(編輯)
實際上,這需要在數據填充時完成。 它似乎不適用於 on selection changed 方法。 因此,在將數據填充到表格中后,您需要遍歷單元格並更改它們選擇的背景以匹配它們的正常背景。 像這樣的東西(語法可能有點不對,我是從我的 vb 代碼轉換過來的):
foreach (datarow r in dgv.rows)
{
foreach (datacell c in r.cells)
{
c.Style.SelectionBackColor = c.Style.BackColor
}
}
在 vb 中說:
Private Sub datagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles datagridview1.SelectionChanged
datagridview1.ClearSelection()
End Sub
搞砸了,這也有效,因為我只想在單擊單元格時更改第 2 列中的單元格背景顏色:
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
Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
Me.DataGridView1.ClearSelection()
End Sub
而已。 但是,如果您仍然想要點擊行/單元格索引或訪問值:
Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
Dim _ht As DataGridView.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
If _ht.Type = DataGridViewHitTestType.Cell Then
Me.DataGridView1.Rows(_ht.RowIndex).Cells(_ht.ColumnIndex).Value = _
"RowIndex = " & _ht.RowIndex & ", " & "ColumnIndex = " & _ht.ColumnIndex
End If
End Sub
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
到目前為止,我看到的答案並沒有給我確切的答案,但它們為我指明了正確的方向。 就我而言,在綁定到數據源后,DGV 選擇並突出顯示了一個我不想要的單元格。 我只想在用戶選擇完整行時突出顯示。
一段時間后,我找到了以下解決方案,這對我來說很好用:
private void datagridview_SelectionChanged(object sender, EventArgs e)
{
var dgv = (DataGridView)sender;
if (dgv.SelectedCells.Count == 1)
{ // hide selection for the single cell
dgv.DefaultCellStyle.SelectionBackColor = dgv.DefaultCellStyle.BackColor;
dgv.DefaultCellStyle.SelectionForeColor = dgv.DefaultCellStyle.ForeColor;
}
else
{ // show the selected cells
dgv.DefaultCellStyle.SelectionBackColor = dgv.RowsDefaultCellStyle.SelectionBackColor;
dgv.DefaultCellStyle.SelectionForeColor = dgv.RowsDefaultCellStyle.SelectionForeColor;
};
}
注意:在我的示例中,我設置了屬性
多選 = 假,只讀 = 真
因為我使用 DGV 只是為了顯示搜索結果。
<DataGrid ItemsSource="{Binding Credits}" x:Name="Grid"
HorizontalAlignment="Left" RowBackground="Transparent">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.