DataGridViewDisableCheckBoxColumn
来源:互联网 发布:欧阳靖被淘汰 知乎 编辑:程序博客网 时间:2024/06/05 05:28
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
namespace eBroker.Leo.W
...{
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
...{
public DataGridViewDisableCheckBoxColumn()
...{
this.CellTemplate = new DataGridViewDisableCheckBoxCell();
}
}
public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
...{
private bool enabledValue;
public bool Enabled
...{
get
...{
return enabledValue;
}
set
...{
enabledValue = value;
}
}
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
...{
DataGridViewDisableCheckBoxCell cell =
(DataGridViewDisableCheckBoxCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
// By default, enable the CheckBox cell.
public DataGridViewDisableCheckBoxCell()
...{
this.enabledValue = true;
}
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
...{
// The checkBox cell is disabled, so paint the border,
// background, and disabled checkBox for the cell.
if (!this.enabledValue)
...{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
...{
SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
...{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
// Calculate the area in which to draw the checkBox.
CheckBoxState state = value != null && (bool)value ?
CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled;
Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
Point center = new Point(cellBounds.X, cellBounds.Y);
center.X += (cellBounds.Width - size.Width) / 2;
center.Y += (cellBounds.Height - size.Height) / 2;
// Draw the disabled checkBox.
CheckBoxRenderer.DrawCheckBox(graphics, center, state);
}
else
...{
// The checkBox cell is enabled, so let the base class
// handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
namespace eBroker.Leo.W
...{
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
...{
public DataGridViewDisableCheckBoxColumn()
...{
this.CellTemplate = new DataGridViewDisableCheckBoxCell();
}
}
public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
...{
private bool enabledValue;
public bool Enabled
...{
get
...{
return enabledValue;
}
set
...{
enabledValue = value;
}
}
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
...{
DataGridViewDisableCheckBoxCell cell =
(DataGridViewDisableCheckBoxCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
// By default, enable the CheckBox cell.
public DataGridViewDisableCheckBoxCell()
...{
this.enabledValue = true;
}
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
...{
// The checkBox cell is disabled, so paint the border,
// background, and disabled checkBox for the cell.
if (!this.enabledValue)
...{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
...{
SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
...{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
// Calculate the area in which to draw the checkBox.
CheckBoxState state = value != null && (bool)value ?
CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled;
Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
Point center = new Point(cellBounds.X, cellBounds.Y);
center.X += (cellBounds.Width - size.Width) / 2;
center.Y += (cellBounds.Height - size.Height) / 2;
// Draw the disabled checkBox.
CheckBoxRenderer.DrawCheckBox(graphics, center, state);
}
else
...{
// The checkBox cell is enabled, so let the base class
// handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}
}
Test:-->>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace eBroker.Leo.W
...{
public partial class Form1 : Form
...{
private DataGridView dataGridView1 = new DataGridView();
public Form1()
...{
InitializeComponent();
this.AutoSize = true;
this.Load += new EventHandler(Form1_Load);
}
public void Form1_Load(object sender, EventArgs e)
...{
DataGridViewCheckBoxColumn column0 =
new DataGridViewCheckBoxColumn();
DataGridViewDisableCheckBoxColumn column1 =
new DataGridViewDisableCheckBoxColumn();
column0.Name = "CheckBoxes";
column1.Name = "DisableCheckBoxes";
dataGridView1.Columns.Add(column0);
dataGridView1.Columns.Add(column1);
dataGridView1.RowCount = 8;
dataGridView1.AutoSize = true;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;
// Set the text for each checkBox.
for (int i = 0; i < dataGridView1.RowCount; i++)
...{
dataGridView1.Rows[i].Cells["DisableCheckBoxes"].Value =
false;
}
dataGridView1.CellValueChanged +=
new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged +=
new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
dataGridView1.CellClick +=
new DataGridViewCellEventHandler(dataGridView1_CellClick);
this.Controls.Add(dataGridView1);
}
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
...{
if (dataGridView1.IsCurrentCellDirty)
...{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
// If a check box cell is clicked, this event handler disables
// or enables the checkBox in the same row as the clicked cell.
public void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
...{
if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
...{
DataGridViewDisableCheckBoxCell checkBoxCell =
(DataGridViewDisableCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["DisableCheckBoxes"];
DataGridViewCheckBoxCell checkCell =
(DataGridViewCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["CheckBoxes"];
checkBoxCell.Enabled = !(Boolean)checkCell.Value;
dataGridView1.Invalidate();
}
}
// If the user clicks on an enabled checkBox cell, this event handler
// reports that the checkBox is enabled.
void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
...{
if (dataGridView1.Columns[e.ColumnIndex].Name == "DisableCheckBoxes")
...{
DataGridViewDisableCheckBoxCell checkBoxCell =
(DataGridViewDisableCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["DisableCheckBoxes"];
MessageBox.Show("checkBox #" + e.RowIndex +
" is " + (checkBoxCell.Enabled ? "enabled" : "disabled"));
// disallow changing the value of a disabled checkBox
checkBoxCell.ReadOnly = !checkBoxCell.Enabled;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace eBroker.Leo.W
...{
public partial class Form1 : Form
...{
private DataGridView dataGridView1 = new DataGridView();
public Form1()
...{
InitializeComponent();
this.AutoSize = true;
this.Load += new EventHandler(Form1_Load);
}
public void Form1_Load(object sender, EventArgs e)
...{
DataGridViewCheckBoxColumn column0 =
new DataGridViewCheckBoxColumn();
DataGridViewDisableCheckBoxColumn column1 =
new DataGridViewDisableCheckBoxColumn();
column0.Name = "CheckBoxes";
column1.Name = "DisableCheckBoxes";
dataGridView1.Columns.Add(column0);
dataGridView1.Columns.Add(column1);
dataGridView1.RowCount = 8;
dataGridView1.AutoSize = true;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;
// Set the text for each checkBox.
for (int i = 0; i < dataGridView1.RowCount; i++)
...{
dataGridView1.Rows[i].Cells["DisableCheckBoxes"].Value =
false;
}
dataGridView1.CellValueChanged +=
new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged +=
new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
dataGridView1.CellClick +=
new DataGridViewCellEventHandler(dataGridView1_CellClick);
this.Controls.Add(dataGridView1);
}
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
...{
if (dataGridView1.IsCurrentCellDirty)
...{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
// If a check box cell is clicked, this event handler disables
// or enables the checkBox in the same row as the clicked cell.
public void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
...{
if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
...{
DataGridViewDisableCheckBoxCell checkBoxCell =
(DataGridViewDisableCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["DisableCheckBoxes"];
DataGridViewCheckBoxCell checkCell =
(DataGridViewCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["CheckBoxes"];
checkBoxCell.Enabled = !(Boolean)checkCell.Value;
dataGridView1.Invalidate();
}
}
// If the user clicks on an enabled checkBox cell, this event handler
// reports that the checkBox is enabled.
void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
...{
if (dataGridView1.Columns[e.ColumnIndex].Name == "DisableCheckBoxes")
...{
DataGridViewDisableCheckBoxCell checkBoxCell =
(DataGridViewDisableCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["DisableCheckBoxes"];
MessageBox.Show("checkBox #" + e.RowIndex +
" is " + (checkBoxCell.Enabled ? "enabled" : "disabled"));
// disallow changing the value of a disabled checkBox
checkBoxCell.ReadOnly = !checkBoxCell.Enabled;
}
}
}
}
- DataGridViewDisableCheckBoxColumn
- DataGridViewDisableCheckBoxColumn
- ARM 网址推荐
- 一位前辈工程师职业发展的忠告
- KETTLE JAVA API学习
- 读马工的《设计与生存》(三)
- js技巧收集(200多个)
- DataGridViewDisableCheckBoxColumn
- 终于决定要写博客了,就当是网络笔记吧。
- html文本截字
- microsoft office 2003不能安装
- 项目开发经验谈(一)
- JAVA 判断EMAIL格式
- Debian中安装内核以及头文件
- C#基础概念二十五问
- mplayer on omap1610: optimization