2008-10-18 5 views
10

C#의 ListView를 사용하여 표를 만듭니다. 나는 프로그래밍 방식으로 특정 셀을 강조 할 수있는 방법을 찾고 싶다. 하나의 셀만 강조 표시하면됩니다.C# ListView 세부 정보, 단일 셀 강조 표시

나는 Owner Drawn 하위 항목을 실험했지만 아래 코드를 사용하여 강조 표시된 셀은 있지만 텍스트는 표시되지 않습니다! 이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 당신의 도움을 주셔서 감사합니다.

//m_PC.Location is the X,Y coordinates of the highlighted cell. 


void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) 
{ 
    if ((e.ItemIndex == m_PC.Location.Y) && (e.Item.SubItems.IndexOf(e.SubItem) == m_PC.Location.X)) 
     e.SubItem.BackColor = Color.Blue; 
    else 
     e.SubItem.BackColor = Color.White; 
    e.DrawBackground(); 
    e.DrawText(); 
} 
+0

Winforms 또는 web? – tvanfosson

답변

14

당신은 소유자 그리기 목록을하지 않고이 작업을 수행 할 수 있습니다 ListViewSubItem 생성자에

// create a new list item with a subitem that has white text on a blue background 
ListViewItem lvi = new ListViewItem("item text"); 
lvi.UseItemStyleForSubItems = false; 
lvi.SubItems.Add(new ListViewItem.ListViewSubItem(lvi, 
    "subitem", Color.White, Color.Blue, lvi.Font)); 

색상의 인수는 하위 항목의 전경과 배경 색상을 제어한다. 여기에서해야 할 중요한 일은 목록 항목에서 UseItemStyleForSubItems을 False로 설정해야합니다. 그렇지 않으면 색상 변경이 무시됩니다.

소유자 변형 솔루션도 효과가 있었지만 배경을 파란색으로 변경하면 텍스트 (전경) 색상을 변경해야한다는 것을 기억해야합니다. 그렇지 않으면 텍스트가보기 어려워집니다.

1

알아 냈어. 특정 하위 항목의 강조 표시를 전환하는 코드는 다음과 같습니다.

listView1.Items[1].UseItemStyleForSubItems = false; 
if (listView1.Items[1].SubItems[10].BackColor == Color.DarkBlue) 
{ 
    listView1.Items[1].SubItems[10].BackColor = Color.White; 
    listView1.Items[1].SubItems[10].ForeColor = Color.Black; 
} 
else 
{ 
    listView1.Items[1].SubItems[10].BackColor = Color.DarkBlue; 
    listView1.Items[1].SubItems[10].ForeColor = Color.White; 
} 
1

제 경우에는 모든 필드를 포함하여 특정 행을 강조하고 싶습니다. 그래서 내 목록보기의 첫 번째 열에있는 "Medicare"행은 전체 행을 강조 표시합니다.

public void HighLightListViewRows(ListView xLst) 
     { 
      for (int i = 0; i < xLst.Items.Count; i++) 
      { 
       if (xLst.Items[i].SubItems[0].Text.ToString() == "Medicare") 
       { 
        for (int x = 0; x < xLst.Items[i].SubItems.Count; x++) 
        { 
         xLst.Items[i].SubItems[x].BackColor = Color.Yellow; 
        } 
       } 
      } 
     }