Colorizing listview rows according to value
-
If I populate a listview with somethinglike:
foreach (DebtorDataSet.DebtorListRow row in debtorDataSet.DebtorList.Rows)
{
ListViewItem item = listView.Items.Add(row.Code);
item.SubItems.Add(name);
item.SubItems.Add(row.IsPostcodeNull() ? "" : row.Postcode);
item.SubItems.Add(row.IsTelephoneNull() ? "" : row.Telephone);
}How can I set the backcolor of a row to red if row.Telephone is null please?
-
If I populate a listview with somethinglike:
foreach (DebtorDataSet.DebtorListRow row in debtorDataSet.DebtorList.Rows)
{
ListViewItem item = listView.Items.Add(row.Code);
item.SubItems.Add(name);
item.SubItems.Add(row.IsPostcodeNull() ? "" : row.Postcode);
item.SubItems.Add(row.IsTelephoneNull() ? "" : row.Telephone);
}How can I set the backcolor of a row to red if row.Telephone is null please?
Well, The method
ListViewItem.SubItems.Add()
can take a simple string to create a subitem, but it also can take aListViewItem.ListViewSubItem
object to do the same thing. So, you can createListViewItem.ListViewSubItem
subitem, set itsBackColor
property to whatever you want, and then add it to theSubItems
collection of yourListViewItem
ListViewItem.ListViewSubItem it1 = new ListViewItem.ListViewSubItem();
it1.BackColor = Color.Red;
item.SubItems.Add(it1);