如何使用其自身的值更改列表视图子项的背景色
如何使用其自身的值以编程方式更改列表视图中单个单元格的背景颜色?
ColorFlag 列中的值来自数据库。
这是我的代码:
foreach(DataRow dr in _dataTbl.Rows)
{
_markOW = dr["Mark"].ToString();
_stock = dr["Stock"].ToString();
_SteelSectio = dr["SteelSection"].ToString();
_colo = (Int32)dr["Color"];
ListViewItem _lvi = new ListViewItem(_markOW);
_lvi.SubItems.AddRange(new string[]{_SteelSectio, _stock, _colo.ToString()});
_myListView.Items.Add(_lvi); }
这是我尝试更改单元格背景色的代码:
for (int _i = 0; _i < _owLV.Items.Count; _i++)
{
_myListView.Items[_i].UseItemStyleForSubItems = false;
_myListView.Items[_i].SubItems[3].BackColor = Color.FromArgb(_colo);
}
提前致谢
How can I programmatically change the back color of a single cell in a listview using its own value?
The values in the ColorFlag Column Came from the database.
Here is my code:
foreach(DataRow dr in _dataTbl.Rows)
{
_markOW = dr["Mark"].ToString();
_stock = dr["Stock"].ToString();
_SteelSectio = dr["SteelSection"].ToString();
_colo = (Int32)dr["Color"];
ListViewItem _lvi = new ListViewItem(_markOW);
_lvi.SubItems.AddRange(new string[]{_SteelSectio, _stock, _colo.ToString()});
_myListView.Items.Add(_lvi); }
Here is the code that I have tried to change the backcolor of the cells:
for (int _i = 0; _i < _owLV.Items.Count; _i++)
{
_myListView.Items[_i].UseItemStyleForSubItems = false;
_myListView.Items[_i].SubItems[3].BackColor = Color.FromArgb(_colo);
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您的代码看起来不错。我只是组合了一个快速的 Windows 窗体应用程序,并在窗体上添加了一个 ListView,并在详细视图中包含两列。下面的代码工作正常。
我会在添加项目之前尝试设置背景颜色。看起来您还将所有项目设置为相同的颜色,这可能不是您想要的。
As far as I can tell, the code you have looks fine. I just threw together a quick Windows Forms application and tossed a ListView on the form with two columns in detail view. The following code works fine.
I would try setting the BackColor before you add the item. It also looks like you're setting all the items to the same color which is probably not what you want.
看一下这些链接:
C# ListView 详细信息,突出显示单个cell
使用 C# 更改列表视图单元格的颜色 (有一个可行的解决方案)
关键点是设置这个:
这样做:
Take a look at these links:
C# ListView Detail, Highlight a single cell
Changing color of list view cell using C# (has a working solution)
The key point is to set this:
Do this: