2014-05-09 44 views
0

DataGridView ..我專注於第一列。在GRID LOAD中突出顯示具有相同值的行。
讓我們假設1的高亮帶有紅色的行,3的高亮帶有藍色的行,5的帶綠色的行。
或1的紅色,3的沒有高亮,5的紅色再次(如替代顏色)。
C#DataGridView高亮行

想法是用相同的值在視覺上分隔行。

任何想法傢伙?提前致謝。

Column1 | 
------ 
1 
1  
3 
3 
3  
5 
5 

試過,但找不出視覺分離:

int i, i_temp = 0; 
foreach (DataGridViewRow dr in dgv.Rows) 
     { 
      i = int.Parse(dr.Cells["Column1"].Value.ToString()); 
      if (i_temp == int.Parse(dr.Cells["Column1"].Value.ToString())) 
      { 
       dr.DefaultCellStyle.BackColor = Color.Red; 
       i_temp = i; 
      } 
     } 

回答

0

的規定的方式做到這一點是在CellFormatting事件。

private void MyGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    var dr = MyGrid.Rows[e.RowIndex]; 
    var intVal = int.Parse(dr.Cells["Column1"].Value.ToString()); 
    switch (intVal) 
    { 
     case 1: 
      e.CellStyle.BackColor = Color.Red; 
      break; 
     case 3: 
      e.CellStyle.BackColor = Color.Blue; 
      break; 
     case 5: 
      e.CellStyle.BackColor = Color.Green; 
      break; 
     default: 
      break; 
    } 
} 

請注意,只有通過在e.ColumnIndex屬性上設置格式條件,才能將其應用於某些列。