Custom ComboBox Column for DataGridView

拈花ヽ惹草 提交于 2019-12-23 02:07:27

问题


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.

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!


回答1:


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;

            }
        }


来源:https://stackoverflow.com/questions/13822925/custom-combobox-column-for-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!