ComboBoxCell中的自定义绘制项目

时间:2013-11-27 14:00:41

标签: c# winforms datagridview

我正在尝试使用DrawItem事件在DataGridView中的ComboBoxCell中绘制项目。以下是我的代码。

更新代码:

private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        int index = dgv.CurrentCell.ColumnIndex;
        if (index == FormatColumnIndex)
        {
            var combobox = e.Control as ComboBox;
            if (combobox == null)
                return;
            combobox.DrawMode = DrawMode.OwnerDrawFixed;
            combobox.DrawItem -= combobox_DrawItem;
            combobox.DrawItem += new DrawItemEventHandler(combobox_DrawItem);
        }            
    }

void combobox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index < 0)
        {
            return;
        }
        int index = dgv.CurrentCell.RowIndex;
        if (index == e.Index)
        {
            DataGridViewComboBoxCell cmbcell = (DataGridViewComboBoxCell)dgv.CurrentRow.Cells["ProductFormat"];

            string productID = dgv.Rows[cmbcell.RowIndex].Cells["ProductID"].Value.ToString();

                string item = cmbcell.Items[e.Index].ToString();
                if (item != null)
                {
                    Font font = new System.Drawing.Font(FontFamily.GenericSansSerif, 8);
                    Brush backgroundColor;
                    Brush textColor;

                    if (e.State == DrawItemState.Selected)
                    {
                        backgroundColor = SystemBrushes.Highlight;
                        textColor = SystemBrushes.HighlightText;
                    }
                    else
                    {
                        backgroundColor = SystemBrushes.Window;
                        textColor = SystemBrushes.WindowText;
                    }
                    if (item == "Preferred" || item == "Other")
                    {
                        font = new Font(font, FontStyle.Bold);
                        backgroundColor = SystemBrushes.Window;
                        textColor = SystemBrushes.WindowText;
                    }                        


                    if (item != "Select" && item != "Preferred" && item != "Other")
                        e.Graphics.DrawString(item, font, textColor, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                    else
                        e.Graphics.DrawString(item, font, textColor, e.Bounds);
                }
            }
        }
    }

项目显示正确,但下拉列表似乎不合适,看起来很尴尬。

此外,当我将鼠标悬停在下拉项目上时,它们似乎再次被涂上,这使它们看起来更黑暗和模糊。我怎样才能解决这个问题?谢谢。

2 个答案:

答案 0 :(得分:1)

您的绘图例程看起来像是将网格的RowIndex与ComboBox项集合的e.Index混淆。不同的事情。

尝试删除此内容:

// int index = dgv.CurrentCell.RowIndex;
// if (index == e.Index) {

就模糊性而言,添加以下行来解决这个问题:

void combobox_DrawItem(object sender, DrawItemEventArgs e) {
  e.DrawBackground();

答案 1 :(得分:0)

我遇到了同样的问题并通过在调用e.Graphics之前将SingleBitPerPixelGridFit的TextRenderingHint属性设置为DrawString来解决此问题:

        e.DrawBackground()
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
        e.Graphics.DrawString(lstr_h, e.Font, lbrush_fore, e.Bounds)
        e.DrawFocusRectangle()