【发布时间】:2015-10-06 14:29:07
【问题描述】:
流行方式 (1, 2) DataGridViewComboBox 中项目的自定义绘制是处理事件 DataGridView1.EditingControlShowing 并在那里设置 DrawItem 事件处理程序:
private void dataGridView1_EditingControlShowing(
object sender,
DataGridViewEditingControlShowingEventArgs e)
{
theBoxCell = (ComboBox) e.Control;
theBoxCell.DrawItem += theBoxCell_DrawItem;
theBoxCell.DrawMode = DrawMode.OwnerDrawVariable;
}
您知道出了什么问题:它使用 控制级事件 来处理列的工作。但是如果我有 50 多个 datagridviews 怎么办?应按列实例处理自定义组合框的绘制,保持控制级别不变。
每列处理的最小实现如下。我唯一的问题是 OnDrawItem() 方法没有被调用。我错过了什么? (您可以看到来自相同类的紫红色背景覆盖正在工作。)
(要重现,只需将以下内容粘贴到类文件中,并将 DataGridViewCustomPaintComboBoxColumn 类型的列添加到您的 DataGridView1。)
public class DataGridViewCustomPaintComboBoxColumn : DataGridViewComboBoxColumn
{
public DataGridViewCustomPaintComboBoxColumn()
{
base.New();
CellTemplate = new DataGridViewCustomPaintComboBoxCell();
}
}
public class DataGridViewCustomPaintComboBoxCell : DataGridViewComboBoxCell
{
public override Type EditType {
get { return typeof(DataGridViewCustomPaintComboBoxEditingControl); }
}
protected override void Paint(...)
{
//painting stuff for incative cells here - works well
}
}
public class DataGridViewCustomPaintComboBoxEditingControl : DataGridViewComboBoxEditingControl
{
public override Color BackColor { // property override only for testing
get { return Color.Fuchsia; } // test value works as expected
set { base.BackColor = value; }
}
protected override void OnPaint(PaintEventArgs e) // never called - why?
{
base.OnPaint(e)
}
protected override void OnDrawItem(DrawItemEventArgs e) // never called - why?
{
base.OnDrawItem(e)
}
}
【问题讨论】:
标签: .net winforms events datagridviewcolumn datagridviewcombobox