1 dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.White; 2 Color tempColor = Color.AntiqueWhite; 3 bool b = true;//标示 4 for (int j = 1; j < dataGridView1.Rows.Count; j++) 5 { 6 if (dataGridView1.Rows[j].Cells[1].Value.ToString() == dataGridView1.Rows[j - 1].Cells[1].Value.ToString()) 7 { 8 dataGridView1.Rows[j].DefaultCellStyle.BackColor = dataGridView1.Rows[j - 1].DefaultCellStyle.BackColor; 9 } 10 else 11 { 12 if (b) 13 { 14 dataGridView1.Rows[j].DefaultCellStyle.BackColor = tempColor; 15 tempColor = Color.White; 16 b = false; 17 } 18 else 19 { 20 dataGridView1.Rows[j].DefaultCellStyle.BackColor = tempColor; 21 tempColor = Color.AntiqueWhite; 22 b = true; 23 } 24 } 25 }