简体   繁体   中英

Display list of students in Datagridview

I have a student class which has all these fields and properties.
studentId, studentFirstname, studentLastname, test1Score, test2Score, studentMajor, typeOfStudent .

I computed the student grades and save them in a file. Now i want to display them in a datagridview, but i want to display: studentId, studentLastname, studentMajor, studentGrade .

Here's what my code looks like:

After i opened the open diaolg and get the file that i want to display. The commented lines are the fields that i don't want to display. in my datagridview.

 while (sLine != null)
                    {
                        string[] parts = sLine.Split(seps);


                        StudentGrade st = new StudentGrade();
                        st.Id = int.Parse(parts[0]);
                        //st.FirstName = parts[1];
                        st.LastName = parts[1];
                        // st.Test1 = int.Parse(parts[3]);
                        // st.Test2 = int.Parse(parts[4]);
                        st.Major = parts[2];
                        // st.TypeOfStudent = parts[6];
                        st.Grade = parts[3];

                        STList.Add(st);

                        sLine = sstr.ReadLine();
                        if (sLine == null)
                            break;
                        if (sLine.Trim() == "")
                            break;
                    }

                    dgStudents.DataSource = STList;
                    dgStudents.Refresh();

                }

You can set Visible property of unwanted columns to false...

dgStudents.Columns["FirstName"].Visible = false;
dgStudents.Columns["Test1"].Visible = false;
dgStudents.Columns["Test2"].Visible = false;
dgStudents.Columns["TypeOfStudent"].Visible = false;

You can select required columns as below to fill the datagridview .

dgStudents.DataSource = STList.Select(col => new StudentGrade
{
    Id = col.Id,
    LastName = col.LastName,
    Major = col.Major,
    Grade = col.Grade
}.ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM