我已经创建了一个自定义datagridview列和单元格,用于禁用或隐藏其控件。
public class DataGridViewDisableTextBoxColumn : DataGridViewTextBoxColumn
{
private bool _textBoxesEnabled = true;
public bool TextBoxesEnabled
{
get
{
return _textBoxesEnabled;
}
set
{
_textBoxesEnabled = value;
if (this.DataGridView == null) return;
foreach (DataGridViewRow row in this.DataGridView.Rows)
{
var cell = row.Cells[this.Index] as DataGridViewDisableTextBoxCell;
if (cell == null) continue;
cell.Enabled = _textBoxesEnabled;
}
}
}
private bool _TextBoxesVisible = true;
public bool TextBoxesVisible
{
get
{
return _TextBoxesVisible;
}
set
{
_TextBoxesVisible = value;
if (this.DataGridView == null) return;
foreach (DataGridViewRow row in this.DataGridView.Rows)
{
var cell = row.Cells[this.Index] as DataGridViewDisableTextBoxCell;
if (cell == null) continue;
cell.TextBoxVisible = _TextBoxesVisible;
}
}
}
public DataGridViewDisableTextBoxColumn()
{
this.CellTemplate = new DataGridViewDisableTextBoxCell();
}
}
public class DataGridViewDisableTextBoxCell : DataGridViewTextBoxCell
{
private bool _textBoxVisibleValue;
public bool TextBoxVisible
{
get
{
var colEnabled = true;
if (this.OwningColumn != null) colEnabled = ((DataGridViewDisableTextBoxColumn)this.OwningColumn).TextBoxesVisible;
return _textBoxVisibleValue && colEnabled;
}
set
{
_textBoxVisibleValue = value;
}
}
private bool enabledValue;
public bool Enabled
{
get
{
var colVisible = true;
if (this.OwningColumn != null) colVisible = ((DataGridViewDisableTextBoxColumn)this.OwningColumn).TextBoxesEnabled;
return enabledValue && colVisible;
}
set
{
enabledValue = value;
}
}
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
DataGridViewDisableTextBoxCell cell =
(DataGridViewDisableTextBoxCell)base.Clone();
cell.Enabled = this.Enabled;
cell.TextBoxVisible = this.TextBoxVisible;
return cell;
}
// By default, enable the cell.
public DataGridViewDisableTextBoxCell()
{
this.enabledValue = true;
this._textBoxVisibleValue = 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)
{
if (!this.Enabled || !this.TextBoxVisible)
{
// 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);
}
if (this.TextBoxVisible && !this.Enabled)
{
// Draw the disabled textbox.
TextBoxRenderer.DrawTextBox(graphics, cellBounds, TextBoxState.Disabled);
}
}
else
{
// The button cell is enabled, so let the base class
// handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}
在列上使用TextBoxesEnabled属性时,此方法似乎工作正常。但是,如果我尝试这样做:
private void gridPins_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var row = gridPins.Rows[e.RowIndex];
var view = row.DataBoundItem as ECUTemplatePin_View;
var cellFontSize = row.Cells[colFontSize.Index] as DataGridViewDisableTextBoxCell;
cellFontSize.Enabled = view.IncludeInPrint;
}
cellFontSize变量为null。看来(从调试中)colFontSize实际上是我的应用程序运行时的标准datagridview文本框单元格,而不是自定义类型,即使它初始化为自定义类型(在.Designer.cs文件中)
任何建议从哪里开始寻找我的错误都将非常感激:)
编辑:colFontSize定义如下:
private DataGridViewDisableTextBoxColumn colFontSize;
this.colFontSize = new DataGridViewDisableTextBoxColumn();
//
// colFontSize
//
this.colFontSize.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
this.colFontSize.DataPropertyName = "FontSize";
this.colFontSize.HeaderText = "Skriftstørrelse";
this.colFontSize.Name = "colFontSize";
this.colFontSize.TextBoxesEnabled = true;
this.colFontSize.TextBoxesVisible = true;
this.colFontSize.Width = 95;