1

テキストの代わりにいくつかの形状を表示するフォームで動作するカスタムコンボボックスがあります。これを行うには、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関数がオーバーライドされていません。似たようなものがあると思いますか?どんな助けでもいただければ幸いです。ありがとう!

4

1 に答える 1

0

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;

            }
        }
于 2012-12-11T16:31:27.763 に答える