You can use an owner-drawn ComboBox
.
For example you can:
- Set DrawMode property to
OwnerDrawFixed
- Set
ItemHeight
property to a suitable height
- Handle DrawItem event and draw items based on your required logic.
Sample Code:
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.comboBox1.ItemHeight = 40;
var db = new TestDBEntities();
this.comboBox1.DataSource = db.Products.ToList();
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index > -1)
{
var name = ((Product)this.comboBox1.Items[e.Index]).Name;
var id = ((Product)this.comboBox1.Items[e.Index]).Id; ;
var price = ((Product)this.comboBox1.Items[e.Index]).Price; ;
if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
else if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
e.Graphics.FillRectangle(SystemBrushes.InactiveCaption, e.Bounds);
else
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
e.Graphics.DrawString(name,
new Font(this.comboBox1.Font, FontStyle.Bold),
Brushes.Blue,
new Rectangle(e.Bounds.Left, e.Bounds.Top,
e.Bounds.Width, this.comboBox1.ItemHeight / 2));
e.Graphics.DrawString(string.Format("Id:{0}", id),
this.comboBox1.Font,
Brushes.Red,
new Rectangle(e.Bounds.Left, e.Bounds.Top + this.comboBox1.ItemHeight / 2,
e.Bounds.Width / 2, this.comboBox1.ItemHeight / 2));
e.Graphics.DrawString(string.Format("Price:{0}", price),
this.comboBox1.Font,
Brushes.Red,
new Rectangle(e.Bounds.Left + e.Bounds.Width / 2,
e.Bounds.Top + this.comboBox1.ItemHeight / 2,
e.Bounds.Width, this.comboBox1.ItemHeight / 2));
}
}
Screenshot: