有没有办法让DataGridView没有选择单元格?我注意到即使失去焦点()它至少有一个活跃的细胞。还有其他模式允许这个吗?或其他一些技巧?
答案 0 :(得分:10)
DataGridView.CurrentCell属性可用于清除焦点矩形。
您可以设置此属性 (DataGridView.CurrentCell)为null 暂时删除焦点 矩形,但当控制 获得焦点和价值 property为null,它是自动的 设置为的值 FirstDisplayedCell属性。
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx
答案 1 :(得分:6)
我发现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
事件处理程序的地方不会影响所需的行为,但我把它留在代码片段中因为我注意到我的需要,至少最好只在数据绑定后附加处理程序,这样我避免为每个绑定的项目引发选择更改事件。
答案 2 :(得分:6)
在选择更改事件上将DataGridView.CurrentCell设置为null的问题是以后的事件(如点击)不会被命中。
对我有用的选项是将选择颜色更改为网格颜色。因此,选择将不可见。
RowsDefaultCellStyle.SelectionBackColor = BackgroundColor;
RowsDefaultCellStyle.SelectionForeColor = ForeColor;
答案 3 :(得分:3)
我花了几个小时找到解决这个问题的方法。这样做:
将以下代码添加到您的类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
importaint代码行是
DataGridView1.CurrentRow.Selected = False
祝你好运!
答案 4 :(得分:3)
我有类似的问题,我按照这种方式:
'初始活动单元格'由dataGridView.ClearSelection()清除。
清除/忽略事件处理程序中的选择,'CellMouseClick'事件。
void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridView dgv = sender as DataGridView;
dgv.ClearSelection();
}
答案 5 :(得分:1)
我知道这是一个老问题,WinForms已被取代(但不过很长一段时间仍然在我们的商店中),所以这仍然与我们相关,我也怀疑其他一些。
我没有摆弄选择或CurrentCell
,而是发现实现只是简单地改变行选择颜色,更适合我们的需要。
此外,我不再需要在失去焦点时跟踪旧的选定单元格,也不必在没有焦点时刷新网格时处理棘手的问题(如计时器)并且旧选择的单元格不能&# 34;恢复"何时返回焦点。
除了上面已经发布的解决方案,我们无法(不想)继承DataGridView
控件,而是选择使用合成。下面的代码显示了用于实现该功能的类,然后是关于如何使用它来代替" attach"对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();
也许这对其他人有帮助。
答案 6 :(得分:1)
在您想要清除焦点/选择时使用DataGridView.ClearSelection()(例如,InitializeComponent,Control.LostFocus,Form.Load)。