문제

C#의 ListView를 사용하여 그리드를 만들고 있습니다. 프로그래밍 방식으로 특정 셀을 강조 할 수있는 방법을 찾고 싶습니다. 나는 하나의 셀을 강조하면됩니다.

나는 소유자 그려진 서브 항목을 실험했지만 아래 코드를 사용하여 강조 표시된 셀을 얻지 만 텍스트는 없습니다! 이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 당신의 도움을 주셔서 감사합니다.

//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();
}
도움이 되었습니까?

해결책

소유자 목록을 작성하지 않고도이 작업을 수행 할 수 있습니다.

// 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 ) );

ListViewSubitem 생성자에 대한 색상 인수는 하위 항목의 전경과 배경색을 제어하고 있습니다. 여기서해야 할 중요한 일은 설정됩니다 UseItemStyleForSubItems 목록 항목에서 False에서는 색상 변경이 무시됩니다.

소유자 드로우 솔루션도 효과가 있었을 것이라고 생각하지만 배경을 파란색으로 변경할 때 텍스트 (전경) 색상을 변경해야합니다. 그렇지 않으면 텍스트를보기가 어렵습니다.

다른 팁

그것을 알아 냈습니다. 다음은 특정 하위 요소의 하이라이트를 전환하는 코드입니다.

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;
}

제 경우에는 모든 필드를 포함하여 특정 행을 강조하고 싶었습니다. 따라서 첫 번째 열에 "Medicare"가있는 ListView의 모든 행은 전체 행을 강조 표시합니다.

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;
                    }
                }
            }
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top