在C# WinForm下做過項目的朋友都知道
上面兩個截圖分別為RadioButton列和支持三種狀態的CheckBox列在DataGridView中的實現效果
下面我看具體來看看如何實現這兩種效果
要實現自定義的DataGridView列
public class DataGridViewDisableCheckBoxCell: DataGridViewCheckBoxCell
{
public bool Enabled { get; set; }
// Override the Clone method so that the Enabled property is copied
public override object Clone()
{
DataGridViewDisableCheckBoxCell cell = (DataGridViewDisableCheckBoxCell)base
cell
return cell;
}
// By default
public DataGridViewDisableCheckBoxCell()
{
this
}
// Three state checkbox column cell
protected override void Paint(Graphics graphics
DataGridViewElementStates elementState
DataGridViewCellStyle cellStyle
{
// The checkBox cell is disabled
if (!this
{
// Draw the cell background
if ((paintParts & DataGridViewPaintParts
{
SolidBrush cellBackground = new SolidBrush(cellStyle
graphics
cellBackground
}
// Draw the cell borders
if ((paintParts & DataGridViewPaintParts
{
PaintBorder(graphics
}
// Calculate the area in which to draw the checkBox
CheckBoxState state = CheckBoxState
Size size = CheckBoxRenderer
Point center = new Point(cellBounds
center
center
// Draw the disabled checkBox
CheckBoxRenderer
}
else
{
// The checkBox cell is enabled
base
}
}
}
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
public DataGridViewDisableCheckBoxColumn()
{
this
}
}
主要是要實現DataGridViewDisableCheckBoxCell的呈現方式
protected override void Paint(Graphics graphics
DataGridViewElementStates elementState
DataGridViewCellStyle cellStyle
{
// Draw the cell background
if ((paintParts & DataGridViewPaintParts
{
SolidBrush cellBackground = new SolidBrush(cellStyle
graphics
cellBackground
}
// Draw the cell borders
if ((paintParts & DataGridViewPaintParts
{
PaintBorder(graphics
}
// Calculate the area in which to draw the checkBox
RadioButtonState state = value != null && (SelectedStatus)value == SelectedStatus
Size size = RadioButtonRenderer
Point center = new Point(cellBounds
center
center
// Draw the disabled checkBox
RadioButtonRenderer
}
使用RadioButtonState代替CheckBoxState
當然
首先我們需要手動修改Form的Designer
public enum SelectedStatus
{
Selected
NoSelected
Indeterminate
}
然後設置CheckBox的TrueValue=SelectedStatus
好了!這個時候運行程序
那就是當我們點擊其中的單選或多選按鈕時它的狀態並不能發生變化
private void dataGridView
{
if (e
{
DataGridViewColumn column = dataGridView
if (column is DataGridViewCheckBoxColumn)
{
DataGridViewDisableCheckBoxCell cell = dataGridView
if (!cell
{
return;
}
if ((SelectedStatus)cell
{
cell
}
else if ((SelectedStatus)cell
{
cell
}
else
{
cell
}
}
}
}
這個是CheckBox的
private void dataGridView
{
if (e
{
DataGridViewColumn column = dataGridView
if (column is DataGridViewCheckBoxColumn)
{
DataGridViewDisableCheckBoxCell cell = dataGridView
if (!cell
{
return;
}
if ((SelectedStatus)cell
{
cell
SetRadioButtonValue(cell);
}
else
{
cell
}
}
}
}
private void SetRadioButtonValue(DataGridViewDisableCheckBoxCell cell)
{
SelectedStatus status = (SelectedStatus)cell
if (status == SelectedStatus
{
status = SelectedStatus
}
else
{
status = SelectedStatus
}
for (int i =
{
DataGridViewDisableCheckBoxCell cel = dataGridView
if (!cel
{
cel
}
}
}
函數SetRadionButtonValue負責修改宿主DataGridView當前列中其它的RadionButton的狀態
在完成這些工作後
最後我會提供整個工程供大家下載
From:http://tw.wingwit.com/Article/program/net/201311/11638.html