lsfphs

CheckBox Header Column For DataGridView

参考文章:http://www.codeproject.com/KB/grid/CheckBoxHeaderCell.aspx

使用有点麻烦,稍微改动一下就可以在设计器中使用, 直接贴代码了!

 

代码
    public partial class SelectAllCheckBoxCell : DataGridViewCheckBoxCell
    {
        
public SelectAllCheckBoxCell()
            : 
base()
        {
            
this.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        
public override object DefaultNewRowValue
        {
            
get
            {
                
return false;
            }
        }
    }

 

 

代码
public partial class SelectAllHeaderCell : 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 SelectAllHeaderCell()
        {
        }

        
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)
        {
            
this.Value = string.Empty;

            
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 +
                
// 4;
            (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;

                
this.DataGridView.InvalidateCell(this);

                
for (int i = 0; i < this.DataGridView.Rows.Count; i++)
                {
                    
this.DataGridView.Rows[i].Cells[this.ColumnIndex].Value = _checked;
                }
                
this.DataGridView.EndEdit();
            }

            
base.OnMouseClick(e);
        }
    }

 

 

代码
public partial class SelectAllCheckBoxColumn : DataGridViewColumn
    {
        
public SelectAllCheckBoxColumn()
            : 
base(new SelectAllCheckBoxCell())
        {
            
this.HeaderCell = new SelectAllHeaderCell();
        }
    }

 

 

posted on 2010-01-28 11:03  lsfphs  阅读(508)  评论(0编辑  收藏  举报