2014-10-26 41 views
1

我做了一個窗體窗體應用程序,並且在我的網格視圖的標題中有一個複選框。我想使主標題複選框檢查所有其他複選框。如何使我的主複選框爲所有其他複選框的默認選項

因此,如果我選中主複選框https://imageshack.com/i/ippzf3rGp,,則應該自動選中以下所有複選框。如果我取消選中主標題複選框,那麼所有下面的複選框應該取消選中。我怎樣才能做到這一點我的代碼如下:

public delegate void CheckBoxClickedHandler(bool state); 
public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs 
{ 
    bool _bChecked; 
    public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked) 
    { 
     _bChecked = bChecked; 
    } 
    public bool Checked 
    { 
     get { return _bChecked; } 
    } 
} 

class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell 
{ 
    Point checkBoxLocation; 
    Size checkBoxSize; 
    bool _checked = false; 
    Point _cellLocation = new Point(); 
    System.Windows.Forms.VisualStyles.CheckBoxState _cbState = 
     System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal; 
    public event CheckBoxClickedHandler OnCheckBoxClicked; 

    public DatagridViewCheckBoxHeaderCell() 
    { 

    } 

    protected override void Paint(System.Drawing.Graphics graphics, 
      System.Drawing.Rectangle clipBounds, 
      System.Drawing.Rectangle cellBounds, 
      int rowIndex, 
      DataGridViewElementStates dataGridViewElementState, 
      object value, 
      object formattedValue, 
      string errorText, 
      DataGridViewCellStyle cellStyle, 
      DataGridViewAdvancedBorderStyle advancedBorderStyle, 
      DataGridViewPaintParts paintParts) 
    { 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, 
       dataGridViewElementState, value, 
       formattedValue, errorText, cellStyle, 
       advancedBorderStyle, paintParts); 
     Point p = new Point(); 
     Size s = CheckBoxRenderer.GetGlyphSize(graphics, 
     System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); 
     p.X = cellBounds.Location.X + 
       (cellBounds.Width/2) - (s.Width/2); 
     p.Y = cellBounds.Location.Y + 
       (cellBounds.Height/2) - (s.Height/2); 
     _cellLocation = cellBounds.Location; 
     checkBoxLocation = p; 
     checkBoxSize = s; 
     if (_checked) 
      _cbState = System.Windows.Forms.VisualStyles. 
        CheckBoxState.CheckedNormal; 
     else 
      _cbState = System.Windows.Forms.VisualStyles. 
        CheckBoxState.UncheckedNormal; 
      CheckBoxRenderer.DrawCheckBox 
        (graphics, checkBoxLocation, _cbState); 
     } 

    protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) 
    { 
     Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y); 
     if (p.X >= checkBoxLocation.X && p.X <= 
       checkBoxLocation.X + checkBoxSize.Width 
       && p.Y >= checkBoxLocation.Y && p.Y <= 
       checkBoxLocation.Y + checkBoxSize.Height) 
     { 
      _checked = !_checked; 
      if (OnCheckBoxClicked != null) 
      { 
       OnCheckBoxClicked(_checked); 
       this.DataGridView.InvalidateCell(this); 
      } 

     } 
     base.OnMouseClick(e); 
    } 
} 
+1

斯泰西我想我已經給出了這個問題的答案。有一個事件可以用來編寫應用網格單元格值來檢查或取消選中狀態的代碼。將在標題複選框狀態中調用的方法更改爲「OnCheckBoxClicked」。在這裏,您可以將所有網格單元格值應用於'_checked'參數值循環。順便說一句,我會在1小時後給你答覆。 – Shell 2014-10-28 02:14:29

+0

@Shell NO SHELL你沒有給我!!!我肯定!關於那個 – 2014-10-28 12:42:45

回答

1

您需要電網分配數據源後處理上filter_table()方法OnCheckBoxClicked事件。

private void filter_table() 
{ 
    .... your code 
    dataGridView1.DataSource = dt; 
    cbHeader = (DatagridViewCheckBoxHeaderCell)dataGridView1.Columns[0].HeaderCell; 
    cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked); 
} 

,並在表單中添加下面的方法(也可以只filter_table()後添加此方法)

private void cbHeader_OnCheckBoxClicked(bool _checked) 
{ 
    for (int i = 0; i < dataGridView1.Rows.Count; i++) 
    { 
     //Give the check box column index instead of 0 in .Cells[0] 
     dataGridView1.Rows[i].Cells[0].Value = _checked; 
    } 
} 
相關問題