2010-12-10 9 views
0

필자는 winforms dataGridView에서 단일 셀의 배경색을 변경하는 방법을 절실히 모색하려고합니다. 두 개의 열이 있습니다. 두 번째 열의 내용을 변경하면이 행의 첫 번째 열에있는 셀에 따라 배경이 적절하게 변경됩니다.windows.forms.datagrid에서 단일 셀의 배경색을 변경하는 방법은 무엇입니까?

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
    { 
     if (e.ColumnIndex != 0 || e.RowIndex == -1) 
      return; 
     if (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() == "Red") 
      e.CellStyle.BackColor = Color.Red; 
     else 
      e.CellStyle.BackColor = Color.White; 
    } 

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex != 1 || e.RowIndex == -1) 
      return; 
     // dataGridView1.Rows[e.RowIndex].Cells[0]. ??? 
    } 

첫 번째 이벤트 핸들러는 첫 번째 열의 셀의 backColor를 그릴 경우 그 셀의 backColor를 설정합니다. 두 번째 이벤트 처리기는 값이 변경된 경우 첫 번째 셀에 페인트하도록 지시해야합니다. 열 너비를 변경하면 올바른 색으로 칠하므로 첫 번째 처리기가 작업을 수행합니다. 그러나 세포 그림을 방아쇠를 당기는 방법?

고맙습니다.

답변

0

:

dataGridView1.InvalidateCell(e.RowIndex, 1); 
+0

정확하게 내가 찾고있는 내용입니다. 고맙습니다. –

0

확인, 여기에 나쁜 해킹 : 내가 CellValueChanged 이벤트 처리기에서

var x = dataGridView1.Columns[0].DefaultCellStyle; 
dataGridView1.Columns[0].DefaultCellStyle = null; 
dataGridView1.Columns[0].DefaultCellStyle = x; 

를 삽입하면

, 전체 첫 번째 열은 다시 칠합니다. 그래서 내 세포도 다시 칠해집니다. 하지만 그게 더럽지 않니?

0

새 셀 스타일 개체를 만들어 원하는 색으로 설정 한 다음 현재 셀에 적용해야합니다.

개인 DataGridViewCellStyle CellStyleGreenBackgnd;

CellStyleGreenBackgnd.BackColor = Color.LightGreen;

dataGridView.CurrentCell.Style.ApplyStyle (CellStyleGreenBackgnd);

내가 편집이 다시 그리기를 트리거 한 것,하지만 이벤트가 편집 한 후 실행되지 않을 경우 다음과 같은 뭔가 문제를 강제 할 수 있어야한다고 생각했을 것이다
0

시도 이.

dataGridView1.Rows[indexhere].Cells[indexhere].Style.ForeColor = Color.Yellow; 
관련 문제