2013-08-30 1 views
0

두 개의 datagridview가 있습니다. 동일한 열 머리글을 사용하지만 셀 데이터가 다릅니다.C에서 다른 값을 가진 DataGridview의 강조 셀 #

첫 번째는 grid_db 입니다. 두 번째 것은 grid_statement입니다.

grid_db 값이 cell [j] 셀의 grid_statement 값과 같지 않은 경우 셀을 강조 표시해야합니다 (빨간색). 나는 다음과 같은

int no_of_col = grid_db.Columns.Count; 
int j; 

for (j = 0; j < no_of_col;) 
{ 
    //if statement value is null replace with ZERO 
    if (grid_statement.Rows[0].Cells[j].Value != null && 
     !string.IsNullOrWhiteSpace(grid_statement.Rows[0].Cells[j].Value.ToString())) 
    { 
     B = grid_statement.Rows[0].Cells[j].Value.ToString(); 
    } 


    //if db value is null replace with zero 
    if (grid_db.Rows[0].Cells[j].Value != null && 
     !string.IsNullOrWhiteSpace(grid_db.Rows[0].Cells[j].Value.ToString())) 
    { 
     A = grid_db.Rows[0].Cells[j].Value.ToString(); 
    } 

    if (A != B) 
    { 
     grid_db.Rows[0].Cells[j].Style.BackColor = Color.Red; 
     grid_statement.Rows[0].Cells[j].Style.BackColor = Color.Red; 

     j++; 
    } 
} 

을 시도하지만 코드 위에 뒀하지 않는 것은 모두 그리드의 모든 열을 강조한다. 도움 말 하시겠습니까?

답변

0
var differentCells = 
     grid_db.Rows.OfType<DataGridViewRow>() 
        .SelectMany(r=>r.Cells.OfType<DataGridViewCell>()) 
        .Where(c=>!grid_statement[c.ColumnIndex,c.RowIndex].Value.Equals(c.Value)); 
//loop through all the cells and make them red 
foreach(var cell in differentCells) 
    cell.Style.BackColor = Color.Red; 
+0

효과가 있습니까? 이것은 코드의 단순한 변형입니다. 나는 그의 코드와 코드를 모두 시험해 보았지만 그 셀을 채색 할 수는 없습니다. 나는 이유를 찾을 수 없다. –

+0

바하 .. 아직 결과가 없습니다. 그의 코드는 또한 모든 셀을 빨간색으로 칠합니다. –

0

내가 코드를했는데, 그것은 나를 위해 작동, 내가 변경 한 유일한 것은 (그 때문에 그것은 단지 1 행에 대해 작동합니다, 그렇지 않으면 쉽게 무한 될 수있는 모든 패스를 증가하는 for 루프입니다 코드가하는 것) :

public Form1() 
    { 
     InitializeComponent(); 

     grid_db.DataSource = new[] 
     { 
      new{ 
       id = 1, 
       tekst="a" 
       }, 
       new 
        { 
         id=2, 
         tekst="b" 
        } 
     }.ToList(); 
     grid_statement.DataSource = new[] 
     { 
      new{ 
       id = 1, 
       tekst="b" 
       }, 
       new 
        { 
         id=2, 
         tekst="c" 
        } 
     }.ToList(); 
     Load += (sender, args) => 
        { 
         HighlightRows(); 
        }; 
    } 
    private void HighlightRows() 
    { 
     int no_of_col = grid_db.Columns.Count; 
     int j; 
     var B = ""; 
     var A = ""; 
     for (j = 0; j < no_of_col; j++) 
     { 
      //if statement value is null replace with ZERO 
      if (grid_statement.Rows[0].Cells[j].Value != null && 
       !string.IsNullOrWhiteSpace(grid_statement.Rows[0].Cells[j].Value.ToString())) 
      { 
       B = grid_statement.Rows[0].Cells[j].Value.ToString(); 
      } 
      //if db value is null replace with zero 
      if (grid_db.Rows[0].Cells[j].Value != null && 
       !string.IsNullOrWhiteSpace(grid_db.Rows[0].Cells[j].Value.ToString())) 
      { 
       A = grid_db.Rows[0].Cells[j].Value.ToString(); 
      } 
      if (A != B) 
      { 
       grid_db.Rows[0].Cells[j].Style.BackColor = Color.Red; 
       grid_statement.Rows[0].Cells[j].Style.BackColor = Color.Red; 

      } 
     } 
    }