datagridview 自定义标题背景图片
datagridview很好用,但样子太差了。
其它还好说,就是标题,不好弄。鼓捣了半天,终于搞定,先记下来,省得忘了。。

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == -1) { int X = e.CellBounds.X; int Y = e.CellBounds.Y; int W = e.CellBounds.Width; int H = e.CellBounds.Height; Pen blackPen = new Pen(Color.FromArgb(195, 195, 195));//边框颜色 Image image = Properties.Resources._title1;//背景图片 TextureBrush tBrush = new TextureBrush(image); e.Graphics.FillRectangle(tBrush, new Rectangle(X, Y, W, H)); if (e.ColumnIndex == 0) e.Graphics.DrawRectangle(blackPen, new Rectangle(X, Y + 1, W - 1, H - 2)); else e.Graphics.DrawRectangle(blackPen, new Rectangle(X - 1, Y + 1, W, H - 2)); e.PaintContent(e.CellBounds); e.Handled = true; } }
第一列。全选实现,园子里很多了。先放这。
#region 重写复选框表头(实现全选) //实现全选的事件参数类 public class DataGridviewCheckboxHeaderEventHander : EventArgs { private bool checkedState = false; public bool CheckedState { get { return checkedState; } set { checkedState = value; } } } //与事件关联的委托 public delegate void DataGridviewCheckboxHeaderCellEventHander(object sender, DataGridviewCheckboxHeaderEventHander e); //重写 DataGridViewColumnHeaderCell public class DataGridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell { private Point checkBoxLocation; private Size checkBoxSize; private bool isChecked = false; private Point cellLocation = new Point(); private System.Windows.Forms.VisualStyles.CheckBoxState cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal; public event DataGridviewCheckboxHeaderCellEventHander OnCheckBoxClicked; //绘制列头checkbox protected override void Paint(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { base.Paint(g, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); Point p = new Point(); Size s = CheckBoxRenderer.GetGlyphSize(g, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); //列头checkbox的X坐标 p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2) - 1; //列头checkbox的Y坐标 p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2); cellLocation = cellBounds.Location; checkBoxLocation = p; checkBoxSize = s; if (isChecked) cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal; else cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal; //绘制复选框 CheckBoxRenderer.DrawCheckBox(g, checkBoxLocation, cbState); } /// <summary> /// 响应点击列头checkbox单击事件 /// </summary> 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) { isChecked = !isChecked; //获取列头checkbox的选择状态 DataGridviewCheckboxHeaderEventHander ex = new DataGridviewCheckboxHeaderEventHander(); ex.CheckedState = isChecked; //此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例 object sender = new object(); if (OnCheckBoxClicked != null) { //触发单击事件 OnCheckBoxClicked(sender, ex); this.DataGridView.InvalidateCell(this); } } base.OnMouseClick(e); } } #endregion
然后在绑定事件后,加入
DataGridviewCheckboxHeaderCell ch = new DataGridviewCheckboxHeaderCell(); ch.OnCheckBoxClicked += new DataGridviewCheckboxHeaderCellEventHander(ch_OnCheckBoxClicked); //关联单击事件 //第一列为DataGridViewCheckBoxColumn DataGridViewCheckBoxColumn checkboxCol = this.dataGridView1.Columns[0] as DataGridViewCheckBoxColumn; //checkboxCol.ThreeState = true; checkboxCol.HeaderCell = ch; checkboxCol.HeaderCell.Value = string.Empty;//消除列头checkbox旁出现的文字
void ch_OnCheckBoxClicked(object sender, Form2.DataGridviewCheckboxHeaderEventHander e) { foreach (DataGridViewRow dgvRow in this.dataGridView1.Rows) { if (e.CheckedState) { dgvRow.Cells[0].Value = true; } else { dgvRow.Cells[0].Value = false; } } }
浙公网安备 33010602011771号