2014-10-10 1 views
1

DataGridView 셀 뒷면 색과 관련된 문제가 있습니다.DataGridView 셀의 후면 컬러를 어떻게 재설정하거나 새로 고칠 수 있습니까?

먼저 datagridview2의 특정 셀의 배경색을 변경합니다.

int cnt = dataGridView1.Rows.Count; 
     for (int i = 0; i < cnt; i++) 
     { 
      string starting = dataGridView1.Rows[i].Cells[9].Value.ToString(); 
      string startings = Convert.ToDateTime(starting.ToString()).ToString("dd MMM yyyy"); 
      string ending = dataGridView1.Rows[i].Cells[10].Value.ToString(); 
      string endings = Convert.ToDateTime(ending.ToString()).ToString("dd MMM yyyy"); 

      TimeSpan ts = Convert.ToDateTime(ending.ToString()) - Convert.ToDateTime(starting.ToString()); 
      double diff = ts.TotalDays + 1; 
      int dif = Convert.ToInt32(diff); 
      for (int d = 0; d < dif; d++) 
      { 
       string str = Convert.ToDateTime(starting.ToString()).AddDays(d).ToString("dd MMM yyyy"); 
       dataGridView2.Rows[i].Cells[str].Style.BackColor = Color.Olive; 
      } 
     }  

DataGridview1에 입력 된 새 값에 따라 다시 색상을 재설정해야합니다. 여기 내 코드가있다.

Private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex == 7 || e.ColumnIndex == 8 || e.ColumnIndex == 9 || e.ColumnIndex == 10) 
     { 
      int prd_per_d = Convert.ToInt32(dataGridView1.CurrentRow.Cells[7].Value); 
      int qty=Convert.ToInt32(dataGridView1.CurrentRow.Cells[6].Value); 
      dataGridView1.CurrentRow.Cells[8].Value = qty/prd_per_d + 5; 

      DateTime sdt = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[9].Value); 
      int req_day = Convert.ToInt32(dataGridView1.CurrentRow.Cells[8].Value); 
      dataGridView1.CurrentRow.Cells[10].Value = sdt.AddDays(req_day); 

      int cnt = dataGridView1.Rows.Count; 
      for (int i = 0; i < cnt; i++) 
      { 
       string starting = dataGridView1.Rows[i].Cells[9].Value.ToString(); 
       string startings = Convert.ToDateTime(starting.ToString()).ToString("dd MMM yyyy"); 
       string ending = dataGridView1.Rows[i].Cells[10].Value.ToString(); 
       string endings = Convert.ToDateTime(ending.ToString()).ToString("dd MMM yyyy"); 

       TimeSpan ts = Convert.ToDateTime(ending.ToString()) - Convert.ToDateTime(starting.ToString()); 
       double diff = ts.TotalDays + 1; 
       int dif = Convert.ToInt32(diff); 

       for (int d = 0; d < dif; d++) 
       { 
        string str = Convert.ToDateTime(starting.ToString()).AddDays(d).ToString("dd MMM yyyy"); 

        dataGridView2.Rows[i].Cells[str].Style.BackColor = Color.Olive; 
       } 
      } 

     } 
    }  

하지만 datagridview1에 값을 입력하면 볼 수있는 변경 사항이 없습니다. 새 셀에서만 작동하지만 기존 컬러 셀은 기본값으로 변경되지 않습니다. 이유가 무엇입니까?

+0

흠, 'CellEndEdit' 이벤트의 업데이트가 누락 되었습니까? 편집 된 셀을 새로 고치기 만하면됩니다. 마지막에 DGV.Refresh 호출을 추가해야할까요? – TaW

+0

아무런 변화가 일어나지 않습니다. – Bappy

+0

흠, 그 셀/열 이름은 확실합니까? 나는 그들이 그 (것)들에있는 공간을 가질다는 것을 의심한다.! 그래도 예외를 throw해야합니다. – TaW

답변

0

약 일주일 전에 동일한 문제가 발생했습니다. 나는 단지 2 행의 programmcode로 해결했다. 희망이 당신을 도울 것입니다! :

 this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque; 
     this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige; 
관련 문제