How can I manually add data to a dataGridView?

前端 未结 5 631
梦谈多话
梦谈多话 2020-12-16 19:23

I\'m trying to run this code, and I get an exception:

Index was out of range. Must be non-negative and less than the size of the collection. Param

5条回答
  •  旧巷少年郎
    2020-12-16 20:01

    Its simple,

    myDataGridView.Rows.Add(value1, value2, value3...);
    

    It worked when I had configured my DGV previously for the coming data columns through the GUI. So in your case, it would be like:

    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)
        {
            dataGridView1.Rows.Add(i.ToString(), student.LastNameFather + " " + student.LastNameMother + ", " + student.Name);
            i++;
        }
    }
    

    May be you have to configure the DGV for the columns and their names separately.

提交回复
热议问题