DataGridView的自定义ComboBox列 [英] Custom ComboBox Column for DataGridView

查看:318
本文介绍了DataGridView的自定义ComboBox列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个自定义ComboBox在一个窗体上显示一些形状而不是文本。要做到这一点,我所要做的是覆盖OnDrawItem函数,它显示我们想要的。下面是一个代码片段供参考:

We have a Custom ComboBox working on a form that displays some shapes instead of text. To do that all I had to do was to override the OnDrawItem function and it displays what we want. Here is a snippet for reference:

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;
        }
    }
}

到一个表单,并添加几个项目到您的集合,所有你看到的是一个正方形和一个圆圈在下拉列表中。

So, if you add that to a form and add a couple of items to your collection all you see is a square and a circle in the drop down.

好的,现在我想做的是将同样的组合框添加到DataGridView。我知道这个控件有一个DataGridViewComboBoxColumn。我试图扩展控件,但是,我看不到这个OnDrawItem函数覆盖。我想有类似的东西吗?
任何帮助将不胜感激。
谢谢!

Ok, so, what I want to do now is add this same combo box to a DataGridView. I know that this control has a DataGridViewComboBoxColumn. I was trying to extend the control, however, I don't see this OnDrawItem function to override. I guess there exists something similar? Any help would be appreciated. Thanks!

推荐答案

您需要将DataGridViewComboBox作为自定义Combobox。

You need to interperet the DataGridViewComboBox as your custom Combobox.

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;

            }
        }

这篇关于DataGridView的自定义ComboBox列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆