質問

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!

役に立ちましたか?

解決

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;

            }
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top