2014-12-31 7 views
2

DataGridView에서 셀 색상을 변경하려고합니다.프로그래밍 방식으로 DataGridview의 색상 셀을 변경하십시오.

저는 datagridview에 조금 익숙하며 많은 질문을 여기에 게시했지만 아무 것도 저에게 효과가없는 것으로 보입니다.

중복으로 표시하지 마십시오.

내 마지막 반복 (여전히 작동하지 않습니다)입니다 :

DataGridView1.AutoGenerateColumns = true; 
     DataGridView1.AutoSize = true; 
     DataGridView1.DataSource = DT; 
     int rowIndex = 0; 
     int cellIndex = 0; 
     Color c = Color.Gray; 
     foreach(var row in DataGridView1.Rows.Cast<DataGridViewRow>().ToList())//.ForEach(row => 
     { 
      var cells = row.Cells; 
      if (cells.Count > 0) { 
       cellIndex = 0; 
       foreach (DataGridViewCell cell in cells) 
       { 
        DataGridView1.Rows[rowIndex].Cells[cellIndex].Style.BackColor = c; 
        //cell.Style.BackColor = c; 
        cellIndex++; 

       } 
       if (cells[0].Value!= null) 
       if ((cells[0].Value as string).Contains("==="))/*end of section*/ 
       { 
        c = (c == Color.Gray) ? Color.Transparent : Color.Gray;/*change color at end of section*/ 
       } 
      } 
      rowIndex++; 
     } 
     rowIndex = 0; 

어떻게 내 코드에 문제가 될 수 있습니까? 색상을 변경하려면 True \ False로 설정해야하는 매개 변수가 있습니까?

편집 나는 무엇을 : 만 DataGridView에 이렇게 데이터가 모든 다른 용도가 컨텍스트 메뉴 (MouseButtons.Right)

더 코드

public partial class ResultsDiffForm : Form 
{ 
    public ResultsDiffForm(DataTable DT)//consatructor 
    { 
     InitializeComponent(); 
     /*Old code goes here ... */ 
    } 
} 
와 것을 새로운 양식을 열려면

내가 뭘하는지 명확하게해야합니다

+0

당신이 ("값을" "스타일")'Styles.Add를 사용하여 시도있다;'? –

+0

@DrewKennedy 시도해 보겠습니다 – LordTitiKaka

+0

명확히하기 위해 CSS 규칙을 사용합니다 :'Styles.Add ("background-color", "gray");' –

답변

1

같은 시도 할 수 있지만, 윈폼에서 DataGridView 컨트롤이 몹시 신경을 쓰는 것입니다 폼의 생성자.

대신 온로드 재정의를 사용해보십시오 :

protected override void OnLoad(EventArgs e) { 
    base.OnLoad(e); 

    // your DataGridView code here... 
} 
+0

내가하고있는 것을 보여주기 위해 더 많은 코드를 게시하겠습니다. 다시보십시오. – LordTitiKaka

+0

You are Right !!! 이제 가장 큰 질문 : 왜? – LordTitiKaka

0

마찬가지로 :

DataGridview1.Columns[index].DefaultCellStyle.BackColor = Color.Gray; 
+0

DefaultCellStyle.BackColor VS Style.BackColor – LordTitiKaka

+0

당신이 그것을 시도한 차이점을 좀 explenation을 추가하시기 바랍니다 수 있습니까? 작동 했나요? 나는 그것이 왜 효과가 있는지 전혀 모른다. .. 그것은 나를 위해 단지 일한다. 그래서 나는 당신에게 그것을 보여 줄 것이다라고 생각했다. – CularBytes

+0

시도해 보았지만 문제가 (폼의 생성자 내에서) @LarsTech가 대답 한 것처럼 보였습니다. 문제가 무엇인지 말할 수 없습니다. – LordTitiKaka

0

당신이 셀 속성을 설정하려고 할 때이 코드를 호출하는 경우 당신은 게시되지 않은이

   if (cells[0].Value!= null) 
       if ((cells[0].Value as string).Contains("==="))/*end of section*/ 
       { 
        if(c == Color.Gray) 
        { 
         c = Color.Transparent;        
        } 
        else 
        {     
         c = Color.Gray; 
        } 
       } 
+0

어떻게 색상을 선택해야할까요? 회색으로 시작하기 때문에 아직 내 테이블이 모두 흰색입니다 – LordTitiKaka

0
int rowIndex = 0; 
     int cellIndex = 0; 
     Color c = Color.Gray; 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      var cells = row.Cells; 
      if (cells.Count > 0) 
      { 
       cellIndex = 0; 
       foreach (DataGridViewCell cell in cells) 
       { 
        dataGridView1.Rows[rowIndex].Cells[cellIndex].Style.BackColor = c; 

        cellIndex++; 

       } 
       if (cells[0].Value != null) 
        if ((cells[0].Value as string).Contains("===")) 
        { 
         c = ((c == Color.Gray) ? Color.Transparent : Color.Gray); 
        } 
      } 
      rowIndex++; 
     } 
     rowIndex = 0; 
+0

희망이 도움이 될 수 있습니다! – RobDev

+0

죄송하지만 내 코드와 당신의 차이점을 알 수는 없지만 ... 정리해 주시겠습니까? – LordTitiKaka

+0

foreach의 반복은 다르게 수행됩니다. 하지만 그것은 당신을 위해 일합니까? – RobDev

관련 문제