2008-12-04 77 views

回答

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事件處理程序不影響所需的行爲,但我把它留在代碼片段中,因爲我注意到我的需要,至少最好只在數據綁定後附加處理程序,以便避免爲每個項目綁定選擇更改事件。

3

我花了好幾個小時才找到解決方案。這樣做:

  1. 創建窗體項目
  2. 添加一個DataGridView名爲 「DataGridView1」
  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 

祝你好運!

6

在選擇更改事件中將DataGridView.CurrentCell設置爲null的問題是後面的事件(如click)不會被擊中。

我工作的選項是將選擇顏色更改爲網格顏色。因此選擇將不可見。

RowsDefaultCellStyle.SelectionBackColor = BackgroundColor; 
RowsDefaultCellStyle.SelectionForeColor = ForeColor; 
+0

您的解決方案不會使DataGridView沒有選中的單元格。這只是出現... – nbadaud 2016-09-02 08:23:03

3

我有類似的問題,我也跟着這樣說:

  1. '初始活動單元格' 通過dataGridView.ClearSelection清除()。

  2. 清除/忽略事件處理程序'CellMouseClick'事件中的選擇。

    void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
    
    { 
    
        DataGridView dgv = sender as DataGridView; 
    
        dgv.ClearSelection(); 
    
    } 
    
1

我知道這是一個老問題和WinForms被取代(但不是很長一段時間仍然在我們的商店無論如何),所以這仍然是有關給我們,我懷疑其他幾個人了。

而不是擺弄選擇或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(); 

也許這對別人有幫助。

相關問題