1 回答
TA贡献1785条经验 获得超4个赞
看起来列是每个班级,而行是每个学生,具体取决于您如何进行平均和最大值。如果是这样,那么您将需要一些内部变量来保持通过阈值,在您的示例中为 50 分。因此,您可以使用 Linq 轻松获得所需的内容。您可能需要检查除以零错误,但这是它的要点:
dataGridView1.Rows.Add(txt_name.Text, txt_grade.Text);
private void btn_calculate_Click(object sender, EventArgs e)
{
int passingMarks = 50;
int[] columnData = (from DataGridViewRow row in dataGridView1.Rows
where row.Cells[1].FormattedValue.ToString()!= string.Empty
select Convert.ToInt32(row.Cells[1].FormattedValue)).ToArray();
lbl_average.Text = "Average grade is: " + columnData.Average().ToString();
lbl_highest.Text = "Highest grade is: " + columnData.Max().ToString();
var passingStudents = columnData.AsQueryable().Where(g=>g >= passingMarks).Count();
var passingRate = ((double)passingStudents / columnData.Length) * 100;
lbl_passrate.Text = String.Format("Pass Rate = {0}%", passingRate);
}
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报