我正在尝试运行这段代码,但出现异常:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
private void LoadStudentGrades(int gradeParaleloId, int subjectId)
{
GradeStudentRepository gradeStudentRepo = new GradeStudentRepository();
students = gradeStudentRepo.FindAllGradeStudents().Where(g => g.GradeParaleloId == gradeParaleloId)
.Select(g => g.Student);
int i = 1;
foreach (var student in students)
{
DataGridViewRow row = new DataGridViewRow();
row.Cells[0].Value = i.ToString();
row.Cells[1].Value = student.LastNameFather + " " + student.LastNameMother + ", " + student.Name;
dataGridView1.Rows.Add(row);
i++;
}
}
我在 datagridview 中手动创建了列,现在我想使用这个小方法填充字段。
最佳答案
你只漏了一行:-P
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1); // this line was missing
row.Cells[0].Value = "Cell1";
row.Cells[1].Value = "Cell2";
dataGridView1.Rows.Add(row);
关于c# - 如何手动将数据添加到 dataGridView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6092463/