combobox的DrawMode为OwnerDrawVariable,重写DrawItem时,是这样写的:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(scomboBox1.Items[e.Index].ToString(),e.Font, new Pen(e.ForeColor).Brush, e.Bounds);
}
但执行后下拉列表框的选项不是想要的内容,而是System.Data.DataRowView。
这是因为数据绑定时用了DataTable,将scomboBox1.Items[e.Index].ToString()修改成((DataRowView)comboBox1.Items[e.Index])["字段名"].ToString()即可。