【发布时间】:2010-12-17 05:54:55
【问题描述】:
如何在 datagridview 中禁用单元格突出显示, 即使我单击单元格,也不会发生突出显示。
大家有什么意见
【问题讨论】:
标签: .net vb.net winforms datagridview
如何在 datagridview 中禁用单元格突出显示, 即使我单击单元格,也不会发生突出显示。
大家有什么意见
【问题讨论】:
标签: .net vb.net winforms datagridview
<DataGrid ItemsSource="{Binding Credits}" x:Name="Grid"
HorizontalAlignment="Left" RowBackground="Transparent">
【讨论】:
到目前为止,我看到的答案并没有准确地告诉我我在寻找什么,但它们为我指明了正确的方向。就我而言,在绑定到数据源后,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;
};
}
注意:在我的示例中,我设置了属性
MultiSelect = false,ReadOnly = true
因为我使用 DGV 只是为了显示搜索结果。
【讨论】:
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
【讨论】:
用vb说话:
Private Sub datagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles datagridview1.SelectionChanged
datagridview1.ClearSelection()
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 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 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
}
}
【讨论】:
进行了一次快速的网络搜索,以了解如何使 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;
}
【讨论】:
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
【讨论】: