有没有办法让 DataGridView 没有选择单元格?我注意到即使它失去了 focus() 它也至少有一个活动单元格。是否有另一种模式允许这样做?或其他一些技巧?
7 回答
DataGridView.CurrentCell 属性可用于清除焦点矩形。
可以将此属性(DataGridView.CurrentCell)设置为null来临时移除焦点矩形,但是当控件接收到焦点并且该属性的值为null时,它会自动设置为FirstDisplayedCell属性的值。
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx
我发现DataGridView.CurrentCell = null
在尝试获取请求的行为时对我不起作用。
我最终使用的是:
private void dgvMyGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (dgvMyGrid.SelectedRows.Count > 0)
{
dgvMyGrid.SelectedRows[0].Selected = false;
}
dgvMyGrid.SelectionChanged += dgvMyGrid_SelectionChanged;
}
它需要在DataBindingComplete
事件处理程序中。
附加SelectionChanged
事件处理程序的位置不会影响所需的行为,但我将其留在代码片段中,因为我注意到至少出于我的需要,最好仅在数据绑定之后附加处理程序,这样我就可以避免引发选择更改事件对于每个绑定的项目。
在选择更改事件上将 DataGridView.CurrentCell 设置为 null 的问题是不会命中以后的事件(如单击)。
对我有用的选项是将选择颜色更改为网格颜色。因此选择将不可见。
RowsDefaultCellStyle.SelectionBackColor = BackgroundColor;
RowsDefaultCellStyle.SelectionForeColor = ForeColor;
我花了几个小时来找到这个问题的解决方案。做这个:
- 创建表单项目
- 添加一个名为“DataGridView1”的 DataGridView
将以下代码添加到您的类 Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dgvRow(17) As DataGridViewRow Dim i As Integer For i = 0 To dgvRow.Length - 1 dgvRow(i) = New DataGridViewRow() dgvRow(i).Height = 16 dgvRow(i).Selected = False dgvRow(i).ReadOnly = True DataGridView1.Rows.Add(dgvRow(i)) DataGridView1.CurrentRow.Selected = False Next End Sub
重要的代码行是
DataGridView1.CurrentRow.Selected = False
祝你好运!
我有类似的问题,我按照这种方式:
由 dataGridView.ClearSelection() 清除的“初始活动单元格”。
清除/忽略事件处理程序“CellMouseClick”事件中的选择。
void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { DataGridView dgv = sender as DataGridView; dgv.ClearSelection(); }
我知道这是一个老问题,WinForms 已被取代(但在我们的商店里仍然没有很长时间),所以这仍然与我们相关,我也怀疑其他一些问题。
而不是摆弄选择 or CurrentCell
,我发现实现简单地更改行选择颜色更适合我们的需要。
此外,我不再需要在失去焦点时跟踪旧的选定单元格,也不必处理在没有焦点时刷新网格(如通过计时器)并且在焦点时无法“恢复”旧选定单元格时的棘手问题被退回。
除了上面已经发布的解决方案之外,我们不能(不想)从DataGridView
控件继承,而是选择使用组合。下面的代码显示了用于实现该功能的类,然后是有关如何使用它将行为“附加”到 DataGridView 的代码。
/// <summary>
/// Responsible for hiding the selection of a DataGridView row when the control loses focus.
/// </summary>
public class DataGridViewHideSelection : IDisposable
{
private readonly DataGridView _dataGridView;
private Color _alternatingRowSelectionBackColor = Color.Empty;
private Color _alternatingRowSelectionForeColor = Color.Empty;
private Color _rowSelectionBackColor = Color.Empty;
private Color _rowSelectionForeColor = Color.Empty;
/// <summary>
/// Initializes a new instance of the <see cref="DataGridViewHideSelection"/> class.
/// </summary>
/// <param name="dataGridView">The data grid view.</param>
public DataGridViewHideSelection( DataGridView dataGridView )
{
if ( dataGridView == null )
throw new ArgumentNullException( "dataGridView" );
_dataGridView = dataGridView;
_dataGridView.Enter += DataGridView_Enter;
_dataGridView.Leave += DataGridView_Leave;
}
/// <summary>
/// Handles the Enter event of the DataGridView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void DataGridView_Enter( object sender, EventArgs e )
{
// Restore original colour
if ( _rowSelectionBackColor != Color.Empty )
_dataGridView.RowsDefaultCellStyle.SelectionBackColor = _rowSelectionBackColor;
if ( _rowSelectionForeColor != Color.Empty )
_dataGridView.RowsDefaultCellStyle.SelectionForeColor = _rowSelectionForeColor;
if ( _alternatingRowSelectionBackColor != Color.Empty )
_dataGridView.AlternatingRowsDefaultCellStyle.SelectionBackColor = _alternatingRowSelectionBackColor;
if ( _alternatingRowSelectionForeColor != Color.Empty )
_dataGridView.AlternatingRowsDefaultCellStyle.SelectionForeColor = _alternatingRowSelectionForeColor;
}
/// <summary>
/// Handles the Leave event of the DataGridView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void DataGridView_Leave( object sender, EventArgs e )
{
// Backup original colour
_rowSelectionBackColor = _dataGridView.RowsDefaultCellStyle.SelectionBackColor;
_rowSelectionForeColor = _dataGridView.RowsDefaultCellStyle.SelectionForeColor;
_alternatingRowSelectionBackColor = _dataGridView.RowsDefaultCellStyle.SelectionBackColor;
_alternatingRowSelectionForeColor = _dataGridView.RowsDefaultCellStyle.SelectionForeColor;
// Change to "blend" in
_dataGridView.RowsDefaultCellStyle.SelectionBackColor = _dataGridView.RowsDefaultCellStyle.BackColor;
_dataGridView.RowsDefaultCellStyle.SelectionForeColor = _dataGridView.RowsDefaultCellStyle.ForeColor;
_dataGridView.AlternatingRowsDefaultCellStyle.SelectionBackColor = _dataGridView.AlternatingRowsDefaultCellStyle.BackColor;
_dataGridView.AlternatingRowsDefaultCellStyle.SelectionForeColor = _dataGridView.AlternatingRowsDefaultCellStyle.ForeColor;
}
#region IDisposable implementation (for root base class)
private bool _disposed;
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <remarks>
/// Called by consumers.
/// </remarks>
public void Dispose()
{
Dispose( true );
GC.SuppressFinalize( this );
}
/// <summary>
/// Disposes this instance, with an indication whether it is called from managed code or the GC's finalization of this instance.
/// </summary>
/// <remarks>
/// Overridden by inheritors.
/// </remarks>
/// <param name="disposingFromManagedCode">if set to <c>true</c> disposing from managed code.</param>
protected virtual void Dispose( Boolean disposingFromManagedCode )
{
if ( _disposed )
return;
// Clean up managed resources here
if ( disposingFromManagedCode )
{
if ( _dataGridView != null )
{
_dataGridView.Enter -= DataGridView_Enter;
_dataGridView.Leave -= DataGridView_Leave;
}
}
// Clean up any unmanaged resources here
// Signal disposal has been done.
_disposed = true;
}
/// <summary>
/// Finalize an instance of the <see cref="DataGridViewHideSelection"/> class.
/// </summary>
~DataGridViewHideSelection()
{
Dispose( false );
}
#endregion
}
/// <summary>
/// Extends data grid view capabilities with additional extension methods.
/// </summary>
public static class DataGridViewExtensions
{
/// <summary>
/// Attaches the hide selection behaviour to the specified DataGridView instance.
/// </summary>
/// <param name="dataGridView">The data grid view.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">dataGridView</exception>
public static DataGridViewHideSelection AttachHideSelectionBehaviour( this DataGridView dataGridView )
{
if ( dataGridView == null )
throw new ArgumentNullException( "dataGridView" );
return new DataGridViewHideSelection( dataGridView );
}
}
用法: 使用实例化 DataGridViewHideSelection 类的实例并在不再需要功能时将其丢弃。
var hideSelection = new DataGridViewHideSelection( myGridView );
// ...
/// When no longer needed
hideSelection.Dispose();
或者,您可以使用方便的扩展方法AttachHideSelectionBehaviour()
让生活更轻松。
myDataGrid.AttachHideSelectionBehaviour();
也许这对其他人有帮助。
使用DataGridView.ClearSelection()来清除焦点/选择(例如 InitializeComponent、Control.LostFocus和Form.Load)。