[英]Custom ComboBox Column for DataGridView
我们有一个自定义组合框,用于显示某些形状而不是文本的表单。 为此,我要做的就是重写OnDrawItem函数,它显示我们想要的。 以下是供参考的摘要:
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
e.DrawBackground();
if (e.Index >= 0)
{
Brush brush = new SolidBrush(Color.LightGray);
int size = this.Height/2;
int origenX = e.Bounds.X + 1;
int origenY = e.Bounds.Y + 3;
System.Drawing.Drawing2D.GraphicsPath path =
new System.Drawing.Drawing2D.GraphicsPath();
switch (e.Index)
{
case 0:
e.Graphics.FillRectangle(brush, origenX, origenY, size, size);
Rectangle r = new Rectangle(origenX, origenY, size, size);
ControlPaint.DrawBorder(e.Graphics, r, Color.Black,
ButtonBorderStyle.Solid);
break;
case 1:
path.AddEllipse(origenX, origenY, size, size);
e.Graphics.FillPath(brush, path);
e.Graphics.DrawPath(Pens.Black, path);
break;
}
}
}
因此,如果将其添加到表单中并向集合中添加几个项目,那么您看到的只是下拉菜单中的正方形和圆形。
好的,所以我现在要做的就是将这个组合框添加到DataGridView中。 我知道此控件具有DataGridViewComboBoxColumn。 我试图扩展控件,但是,看不到要重写的OnDrawItem函数。 我猜有类似的东西吗? 任何帮助,将不胜感激。 谢谢!
您需要交互操作DataGridViewComboBox作为自定义组合框。
private void dgTest_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgTest.CurrentCell.ColumnIndex == 0) // Which column ever is your DataGridComboBoxColumn
{
// This line will enable you to use the DataDridViewCOmboBox like your
// Custom ComboBox.
CustomComboBox combo = e.Control as CUstomComboBox;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.