2

我有一个程序可以在某些条件下动态添加行,我尝试以这种方式实现该功能:

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);
    }
}

使用此代码,它确实可以添加行,但是它们都是空的,所有这些行中都没有数据。(已确认,lessoncourse不是Teachernull)有人可以帮忙吗?谢谢!

4

2 回答 2

3

我终于自己弄明白了,我把它换成row.SetValuesrow.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);
}

使用此代码,它将运行良好。感谢大家的帮助!

于 2013-07-26T11:38:02.973 回答
2

您需要省略该using部分。之后,row对象将被处理:

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);
}
于 2013-07-26T04:41:02.003 回答