c# - ListBox 项目在 OwnerDrawFixed 模式下不显示
问题描述
拜托,我真的很感激任何人都可以帮助我解决以下问题。
我有一个 Access 数据库,我想将它加载到ListBox中。
我将 ListBox 设置DrawMode
为OwnerDrawFixed
,但是当我运行应用程序时,它显示(System.Data.DataRowView)
而不是所有数据库记录。
我必须说它在 Normal DrawMode 中运行良好。
这是我的代码:
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();
}
再次感谢你。
解决方案
我建议对数据的加载方式和用于填充 ListBox 的 Items 集合的方式以及该DrawItems
方法执行项目绘制的方式进行这些更改:
- 使用OleDbDataAdapter
DataTable
代替OleDbDataReader
. _ 它使用简单,它会为您打开连接。 - 返回 a
DataTable
,然后使用第一个 Column 的名称作为 ListBoxDisplayMember
,因此您不需要硬编码(此处)提供要显示的信息的 Field 的名称。您只能在一个地方获得此参考资料(不易出错)。 - 删除
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();
}
推荐阅读
- python - 将 MinMaxScaled 预测和目标与重新缩放的预测和目标进行比较时的相对误差差异
- python - 首先从进程中获取 PID
- django - Django 连接游标范围
- facebook - Facebook Conversion API - 不接收事件
- javascript - 静态鼠标不会主动悬停在旋转立方体上(相对面)
- github - Github Action 无法评论 PR
- vba - 参考 VBA 中缺少 MSCommLib.TWD
- linux - 将项目文件夹从 Windows 移动到 linux 时修复依赖关系树
- python - 使用 Selenium Python 验证 Pin-Pad 表单
- php - 如何在表codeigniter中加入图像