How can I modify the style of a specific cell in a C# ListView?
To modify the style of a cell in a ListView, you can use the SubItems property of the ListView to access the cell and change its style. Here is an example:
// 设置ListView为Details模式
listView1.View = View.Details;
// 添加列头
listView1.Columns.Add("姓名");
listView1.Columns.Add("年龄");
// 添加行数据
ListViewItem item = new ListViewItem("张三");
item.SubItems.Add("25");
listView1.Items.Add(item);
// 修改某个单元格的样式
listView1.Items[0].SubItems[1].ForeColor = Color.Red;
listView1.Items[0].SubItems[1].Font = new Font(listView1.Font, FontStyle.Bold);
In the given example, we first set the ListView to Details mode and added two column headers. Next, we created a ListViewItem object and added it to the ListView as a row of data. Finally, we used listView1.Items[0].SubItems[1] to access the second cell of the first row, changed its foreground color to red, and set it to bold style.
Note that when accessing cells with the SubItems property, the index starts from 1, not 0. The first cell corresponds to the ListViewItem object itself.