2012-11-13 39 views
1

我如何訂閱CheckBox的事件處理程序中類似CheckChanged DataGridView的一個CheckBoxColumn或點擊常規CheckBox的事件處理程序?我有一個或多個列,在我的應用程序中的多個數據網格中,我想要這樣做。DataGridView的複選框單元格/列事件

我已經看了CellContentClick和CellClick,但他們似乎完全不雅,因爲他們火在數據網格中每一次點擊,不只是爲感興趣的細胞或複選框。

+3

可能的重複:http://stackoverflow.com/questions/932040/triggering-a-checkbox-value-changed-event-in-datagridview-c-net或http://stackoverflow.com/questions/11843488/datagridview-checkbox-event –

+0

是的,一個可能的重複,但我相信有一個有趣的解決方案。我很長一段時間的讀者,但一個新的海報StackOverflow。我只是想在這裏貢獻一下。對於一個長線程發佈又一個答案,它會在噪聲中迷失方向嗎? – gmlobdell

+0

我還沒有看到任何聰明的方式來做到這一點(作爲一個誰想要做同樣的事情)。 'EditControl'功能不被這個列類型使用,'DataGridViewCheckboxCell'沒有任何有趣的事件。我認爲如果你正在尋找其他信息,只要你明確說明,一個新問題就沒有問題。 –

回答

3

該解決方案從下面傳來澆築在MSDN文檔和一些在這裏和CodeProject的切向線程運氣。

的想法是創建一個類,從DataGridViewCheckBoxCell衍生,包括與該ContentClick沿觸發的處理程序。這看起來像是一個DataGridView的開銷很大,但是我的應用程序有很多DataGridViews,所以這個代碼是可重用的,從而節省了時間。

/// <summary> 
/// DataGridView cell class for check box cells with a OnContentClick event handler. 
/// </summary> 
public class DataGridViewEventCheckBoxCell : DataGridViewCheckBoxCell 
{ 
    /// <summary> 
    /// Event handler for OnContentClick event. 
    /// </summary> 
    protected EventHandler<DataGridViewCellEventArgs> ContentClickEventHandler { get; set; } 

    /// <summary> 
    /// Empty constructor. Required. Used by Clone mechanism 
    /// </summary> 
    public DataGridViewEventCheckBoxCell() 
     : base() 
    { } 

    /// <summary> 
    /// Pass through constructor for threeState parameter. 
    /// </summary> 
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param> 
    public DataGridViewEventCheckBoxCell(bool threeState) 
     : base(threeState) 
    { } 

    /// <summary> 
    /// Constructor to set the OnContentClick event handler. 
    /// Signature for handler should be (object sender, DataGridViewCellEventArgs e) 
    /// The sender will be the DataGridViewCell that is clicked. 
    /// </summary> 
    /// <param name="handler">Handler for OnContentClick event</param> 
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param> 
    public DataGridViewEventCheckBoxCell(EventHandler<DataGridViewCellEventArgs> handler, bool threeState) 
     : base(threeState) 
    { 
     ContentClickEventHandler = handler; 
    } 

    /// <summary> 
    /// Clone method override. Required so CheckEventHandler property is cloned. 
    /// Individual DataGridViewCells are cloned from the DataGridViewColumn.CellTemplate 
    /// </summary> 
    /// <returns></returns> 
    public override object Clone() 
    { 
     DataGridViewEventCheckBoxCell clone = (DataGridViewEventCheckBoxCell)base.Clone(); 
     clone.ContentClickEventHandler = ContentClickEventHandler; 
     return clone; 
    } 

    /// <summary> 
    /// Override implementing OnContentClick event propagation 
    /// </summary> 
    /// <param name="e">Event arg object, which contains row and column indexes.</param> 
    protected override void OnContentClick(DataGridViewCellEventArgs e) 
    { 
     base.OnContentClick(e); 
     if (ContentClickEventHandler != null) 
      ContentClickEventHandler(this, e); 
    } 

    /// <summary> 
    /// Override implementing OnContentDoubleClick event propagation 
    /// Required so fast clicks are handled properly. 
    /// </summary> 
    /// <param name="e">Event arg object, which contains row and column indexes.</param> 
    protected override void OnContentDoubleClick(DataGridViewCellEventArgs e) 
    { 
     base.OnContentDoubleClick(e); 
     if (ContentClickEventHandler != null) 
      ContentClickEventHandler(this, e); 
    } 
} 

因爲我希望能夠從一列類引用此單元格類,我還實施了從DataGridViewCheckBoxColumn派生的類:

/// <summary> 
/// DataGridView column class for a check box column with cells that have an OnContentClick handler. 
/// </summary> 
public class DataGridViewEventCheckBoxColumn : DataGridViewCheckBoxColumn 
{ 
    /// <summary> 
    /// Empty constructor. Pass through to base constructor 
    /// </summary> 
    public DataGridViewEventCheckBoxColumn() 
     : base() 
    { } 

    /// <summary> 
    /// Pass through to base constructor with threeState parameter 
    /// </summary> 
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param> 
    public DataGridViewEventCheckBoxColumn(bool threeState) 
     : base(threeState) 
    { } 

    /// <summary> 
    /// Constructor for setting the OnContentClick event handler for the cell template. 
    /// Note that the handler will be called for all clicks, even if the DataGridView is ReadOnly. 
    /// For the "new" state of the checkbox, use the EditedFormattedValue property of the cell. 
    /// </summary> 
    /// <param name="handler">Event handler for OnContentClick.</param> 
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param> 
    public DataGridViewEventCheckBoxColumn(EventHandler<DataGridViewCellEventArgs> handler, bool threeState) 
     : base(threeState) 
    { 
     CellTemplate = new DataGridViewEventCheckBoxCell(handler, threeState); 
    } 
} 

修剪掉多餘的代碼,我使用它像此:

public void AddCheckBoxColumn(DataGridView grid, EventHandler<DataGridViewCellEventArgs> handler, bool threeState) 
{ 
    grid.Columns.Add(new DataGridViewEventCheckBoxColumn(handler, threeState)); 
} 

柱類可以被消除,並且它可以被使用如下:

public void AddCheckBoxColumn(DataGridView grid, EventHandler<DataGridViewCellEventArgs> handler, bool threeState) 
{ 
    DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn(threeState); 
    column.CellTemplate = new DataGridViewEventCheckBoxCell(handler, threeState); 
    grid.Columns.Add(column); 
} 

下面是一個示例事件處理程序。數據網格中另一列的狀態將根據此列的值以及WasReleased屬性中的某些狀態信息進行更新。 DataGridView的DataSource是一個Specimen對象的集合,所以每行的DataBoundItem都是一個樣本。樣本類是特定於應用程序的,但它具有由此列顯示的屬性OnHold; IsReleased,由另一列顯示;並被釋放。

public static void OnHoldCheckClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (sender is DataGridViewEventCheckBoxCell) 
     { 
      DataGridViewEventCheckBoxCell cell = sender as DataGridViewEventCheckBoxCell; 

      if (!cell.ReadOnly) 
      { 
       // The rows in the DataGridView are bound to Specimen objects 
       Specimen specimen = (Specimen)cell.OwningRow.DataBoundItem; 
       // Modify the underlying data source 
       if ((bool)cell.EditedFormattedValue) 
        specimen.IsReleased = false; 
       else if (specimen.WasReleased) 
        specimen.IsReleased = true; 
       // Then invalidate the cell in the other column to force it to redraw 
       DataGridViewCell releasedCell = cell.OwningRow.Cells["IsReleased"]; 
       cell.DataGridView.InvalidateCell(releasedCell); 
      } 
     } 
    } 

良好的風格可能會推動事件處理程序中的一些代碼放到樣本類中的一個方法中。

+0

+1創造力。但是,對於我自己,我只有一個datagridview,這是投入大量時間/資源來進行事件。 Patrick Quirk的第二個鏈接似乎是更快的解決方案。試一試。 – Encryption

1

可以使用

e.Item.Cells [1]。文本

e是當前行和第二單元記得小區索引始終以0指數開始

+0

e通常是EventArgs類型的EventHandler參數或從EventArgs派生的類型。這有什麼幫助?你將訂閱什麼事件,以及什麼對象/控件,以便使用(對象發件人,DataGridViewCellEventArgs e)調用事件處理程序。通常,發件人將是行或可從中獲取行的其他對象。請編輯以提供更多詳細信息。 – gmlobdell

+0

event args .... – 2012-11-13 01:53:37

相關問題