2015-08-03 45 views
0

我试图操纵我的dataGridView来查看只有失败的人。如果他/她通过了特定的测试,它应该显示不令人满意的标记和Pass。操纵datagridView并按条件查看

我越来越和DataGridView的默认错误对话框

try 
{ 
    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
    { 
      if ((int)dataGridView1.Rows[i].Cells[1].Value >= 50) 
      dataGridView1.Rows[i].Cells[1].Value = "Pass"; 

      else if ((int)dataGridView1.Rows[i].Cells[2].Value >= 50) 
        dataGridView1.Rows[i].Cells[1].Value = "Pass"; 

      else if ((int)dataGridView1.Rows[i].Cells[3].Value >= 50) 
        dataGridView1.Rows[i].Cells[1].Value = "Pass"; 

      else if ((int)dataGridView1.Rows[i].Cells[4].Value >= 50) 
        dataGridView1.Rows[i].Cells[1].Value = "Pass"; 
    } 
} 
catch (Exception err) 
{ 
    throw; 
} 
+1

而不是改变/操纵在datagridview的数据,你应该改变它在数据表或datagridview的数据源。 – learningNew

回答

0

您可以使用CellFormatting事件:

void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    //if (e.ColumnIndex >= 1 && e.ColumnIndex <= 4) 
     if ((int)e.Value >= 50) 
      e.Value = "Pass"; 
}