【发布时间】:2016-06-18 19:13:09
【问题描述】:
我在 ListView 中有三列,我想将第三列中的每个值都更改为绿色前景色。
用户名、电子邮件、状态
这是我的三列,所有列的名称也如所见。
我尝试过类似的方法但失败了,因为它只是将它添加到列表视图中,而不是在添加到列表视图时更改第三列值的颜色。
var Status = new ListViewItem("Status");
Status.SubItems[0].ForeColor = Color.FromArgb(-16711936);
Status.UseItemStyleForSubItems = false;
Pless_ListView.Items.Add(Status);
我也试过这么简单的方法,还是不行。
Pless_ListView.Items[2].ForeColor = Color.Green;
我也尝试过编辑它,将 Status 值添加到 ListView 中,如下所示。
account.SubItems.Add(Status).ForeColor = Color.Green;
这个也没有运气。
我也尝试过搜索,但不知道在哪里可以找到答案。
编辑:
这是我添加到 ListView 的代码。
void ColorLvColumn(ListView lv, int columnIndex, Color color, bool foreground)
{
foreach (ListViewItem lvi in lv.Items)
{
lvi.UseItemStyleForSubItems = false; // allow individual styles for subitems!
if (columnIndex < lvi.SubItems.Count)
{
if (foreground) lvi.SubItems[columnIndex].ForeColor = color;
//else lvi.SubItems[columnIndex].BackColor = color;
}
}
}
private void addAccountToPlessLV(String Username, String Password, PlessHTTP captured)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate
{
ColorLvColumn(Pless_ListView, 2, Color.Green, true);
ListViewItem account = new ListViewItem { Text = Username };
account.SubItems.Add(Email);
account.SubItems.Add(captured.Status).BackColor = Color.Green;
Pless_ListView.Items.Add(account);
}));
}
else
{
ColorLvColumn(Pless_ListView, 2, Color.Green, true);
ListViewItem account = new ListViewItem { Text = Username };
account.SubItems.Add(Email);
account.SubItems.Add(captured.Status)
Pless_ListView.Items.Add(account);
}
}
【问题讨论】: