簡體   English   中英

如何更改 ObjectListView 中單個單元格的 BackColor/ForeColor

[英]how to change BackColor/ForeColor of a single cell in ObjectListView

我正在嘗試在ObjectListView中設置單元格的 ForeColor 。 在 SO 和其他網站上有關於此的問題和答案,建議使用ObjectListViewFormatCell事件。 http://objectlistview.sourceforge.net/cs/recipes.html#how-can-i-change-the-colours-of-a-row-or-just-a-cell中所述

我嘗試了代碼並且它可以工作,但是當需要重繪單元格時它可以工作(它們最初顯示為黑色,並且我將鼠標移到它上面的每個項目都會調用事件)。 但我真的不需要一個事件,因為我想設置一個固定的顏色,比如:

 foreach (OLVListItem item in olv.Items)
      if (item.SubItems[7].Text != "")
      {
          if (item.SubItems[7].Text.StartsWith("-"))
              item.SubItems[7].ForeColor = Color.Red;
          else item.SubItems[7].ForeColor = Color.DarkGreen;
      }

但上面的代碼不會影響結果。

我意外地通過它的屬性找到了答案。 我將此添加為答案,以便它可以幫助其他人。 OLVListItem有一個UseItemStyleForSubItems屬性,默認情況下為true ,它為其項目使用與ObjectListView相同的 Font、ForeColor 和 BackColor。 將其值設置為 false 有效:

foreach (OLVListItem item in olv.Items)
      if (item.SubItems[7].Text != "")
      {
         item.UseItemStyleForSubItems = false;
          if (item.SubItems[7].Text.StartsWith("-"))
              item.SubItems[7].ForeColor = Color.Red;
          else item.SubItems[7].ForeColor = Color.DarkGreen;
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM