DataBound ComboBox Items BackColor Question

Hi

I originally posted this question in the wrong forum so am reposting here.

I have a Windows.Forms.ComboBox which is databound to a generic collection, see code below:

cboCertificate.ValueMember = "ID";

cboCertificate.DisplayMember = "Description";

ExamPreps.Win.UI.BLL.GenericCollection<ExamPreps.Win.UI.BLL.Certificate> obj = new ExamPreps.Win.UI.BLL.GenericCollection<ExamPreps.Win.UI.BLL.Certificate>();

obj.Add(new ExamPreps.Win.UI.BLL.Certificate(0, "** Please Select **"));

IDataReader Dr = ExamPreps.Win.UI.DAL.GenericDataAccess.GetReader("CertificateList", new SqlParameter[] { });

while (Dr.Read())

{

obj.Add(new ExamPreps.Win.UI.BLL.Certificate(Dr.GetInt32(0), Dr.GetString(1)));

}

Dr.Dispose();

cboCertificate.DataSource = obj;

For each item in my collection where Complete = true, I need to set the BackColour of the ListItems in the ComboBox to Green; is there an event where I can capture the item being added similar to the OnItemDataBound event in ASP.NET

Is there an alternative method to achieve the same thing

Thanks for any help

Smeat




Answer this question

DataBound ComboBox Items BackColor Question

  • etrynus

    You can do this by creating owner drawn combo box:

    public class XComboBox : ComboBox

    {

    /// <summary>

    /// Intializes a new instance of the XComboBox class

    /// </summary>

    public XComboBox()

    {

    DrawMode = DrawMode.OwnerDrawFixed;

    }

    protected override void OnDrawItem(DrawItemEventArgs e)

    {

    base.OnDrawItem(e);

    if (e.Index > -1 && e.Index < Items.Count)

    {

    // If <some condition>, change the backcolor

    if (Items[e.Index].ToString().Contains("1"))

    e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);

    else

    e.DrawBackground();

    using (Brush textBrush = new SolidBrush(e.ForeColor))

    {

    e.Graphics.DrawString(Items[e.Index].ToString(), this.Font, textBrush, e.Bounds);

    }

    e.DrawFocusRectangle();

    }

    }

    }

    The following example paints the background of items that has 1 in them in blue,



  • Nigel36

    Greeaaattttt

    Eli, thanks for such a detailed answer



  • Matthew Langley

    Anytime

  • DataBound ComboBox Items BackColor Question