문제

I have a program that will add rows dynamically in certain conditions, I've tried implement the function in this way:

if (student.UUID == AppliedStudent)
{
    using (DataGridViewRow row = new DataGridViewRow())
    {
        row.SetValues(new object[] { lesson.Name, Course.Course_Name, lesson.Level, lesson.Time, Teacher.C_Name, lesson.Price, Classroom.Classroom_Name });
        row.DefaultCellStyle.BackColor = Color.LightGreen;
        row.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
        dataGridView1.Rows.Add(row);
    }
}

With this code, it can add in rows indeed however they are all empty, all those rows has no data in it. (It has been confirmed that lesson, course and Teacher aren't null.) Can anyone help? Thanks!

도움이 되었습니까?

해결책

I figured it out by myself at last, I replaced the row.SetValues to row.CreateCells:

if (student.UUID == AppliedStudent)
{
    DataGridViewRow row = new DataGridViewRow();
    row.CreateCells(dataGridView1, lesson.Name, Course.Course_Name, lesson.Level, lesson.Time, Teacher.C_Name, lesson.Price, Classroom.Classroom_Name);
    row.DefaultCellStyle.BackColor = Color.LightGreen;
    row.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
    dataGridView1.Rows.Add(row);
}

With this code it will work well. Thank you all for trying to help!

다른 팁

You need to leave out the usingpart. After it the rowobject will be disposed:

if (student.UUID == AppliedStudent)
{
    DataGridViewRow row = new DataGridViewRow();
    row.SetValues(new object[] { lesson.Name, Course.Course_Name, lesson.Level, lesson.Time, Teacher.C_Name, lesson.Price, Classroom.Classroom_Name });
    row.DefaultCellStyle.BackColor = Color.LightGreen;
    row.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
    dataGridView1.Rows.Add(row);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top