首页 > 解决方案 > ListBox 项目在 OwnerDrawFixed 模式下不显示

问题描述

拜托,我真的很感激任何人都可以帮助我解决以下问题。

我有一个 Access 数据库,我想将它加载到ListBox中。
我将 ListBox 设置DrawModeOwnerDrawFixed,但是当我运行应用程序时,它显示(System.Data.DataRowView)而不是所有数据库记录。

我必须说它在 Normal DrawMode 中运行良好。

ListBox OwnerDrawFixe 模式问题图片

这是我的代码:

private void Form1_Load(object sender, EventArgs e)
    {
        ListBox1.DataSource = GetData();
        ListBox1.DisplayMember = "empName";
    }

DataTable dt;

private DataTable GetData()
    {
        dt = new DataTable();
        using (OleDbConnection myConn = new OleDbConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
        {
            using (OleDbCommand myQuery = new OleDbCommand("select empName from empTable", myConn))
            {
                myConn.Open();
                OleDbDataReader myReader = myQuery.ExecuteReader();
                dt.Load(myReader);
            }
        }
        return dt;
    }


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

        bool isItemSelected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
        int itemIndex = e.Index;
        if (itemIndex >= 0 && itemIndex < ListBox1.Items.Count)
        {
            Graphics g = e.Graphics;

            SolidBrush backgroundColorBrush = new SolidBrush((isItemSelected) ? Color.FromArgb(255, 64, 64, 64) : Color.FromArgb(0,64,64,64)); 
            g.FillRectangle(backgroundColorBrush, e.Bounds);

            // Set text color
            string itemText = ListBox1.Items[itemIndex].ToString();

            SolidBrush itemTextColorBrush = (isItemSelected) ? new SolidBrush(Color.White) : new SolidBrush(Color.LightGray); 
            g.DrawString(itemText, e.Font, itemTextColorBrush, ListBox1.GetItemRectangle(itemIndex).Location);

            // Clean up
            backgroundColorBrush.Dispose();
            itemTextColorBrush.Dispose();
        }
        e.DrawFocusRectangle();
    }

再次感谢你。

标签: c#.netwinformsms-accessgraphics

解决方案


我建议对数据的加载方式和用于填充 ListBox 的 Items 集合的方式以及该DrawItems方法执行项目绘制的方式进行这些更改:

  1. 使用OleDbDataAdapterDataTable代替OleDbDataReader. _ 它使用简单,它会为您打开连接。
  2. 返回 a DataTable,然后使用第一个 Column 的名称作为 ListBox DisplayMember,因此您不需要硬编码(此处)提供要显示的信息的 Field 的名称。您只能在一个地方获得此参考资料(不易出错)。
  3. 删除DrawItem方法中不必要的部分,并使用GetItemText()方法检索表示ListControl项目文本的字符串。

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    var dt = GetData();
    if (dt != null) {
        listBox1.DisplayMember = dt.Columns[0].ColumnName;
        listBox1.DataSource = dt;
    }
}

private DataTable GetData()
{
    using (var conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
    using (var cmd = new OleDbCommand("SELECT empName FROM empTable", conn)) {
        conn.Open();
        using (var reader = cmd.ExecuteReader()) {
            if (!reader.HasRows) return null;
            var dt = new DataTable();
            dt.Load(reader);
            return dt;
        }
    }
}

private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) return;
    var lbx = sender as ListBox;
    bool isItemSelected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
    using (SolidBrush bgBrush = new SolidBrush(isItemSelected ? Color.FromArgb(64, 64, 64) : lbx.BackColor))
    using (SolidBrush itemBrush = isItemSelected ? new SolidBrush(lbx.ForeColor) : new SolidBrush(Color.LightGray)) {
        string itemText = lbx.GetItemText(lbx.Items[e.Index]);
        e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        e.Graphics.FillRectangle(bgBrush, e.Bounds);
        e.Graphics.DrawString(itemText, e.Font, itemBrush, e.Bounds);
    }
    e.DrawFocusRectangle();
}

推荐阅读