开发者

How to elegantly tackle this Attendance Form problem?

How to elegantly tackle this Attendance Form problem?

Basically, I want to display a list of all the students (have their names on the far left side), and on each row (each student) display each date for the month selected in the comboBox.

I'm kind of stumped on how to tackle this.

Here's what I have so far:

//cmbMes is the combobox that holds each month to be selected.
private void cmbMes_SelectedIndexChanged(object sender, EventArgs e)
{
    dataGridView1.DataSource = null;
    int gradeParaleloId = Convert.ToInt32(cmbGradeParalelo.SelectedValue);
    var studentRepo = new StudentRepository();
    var students = studentRepo.FindAllStudentsFromGradeParalelo(gradeParaleloId);
    var rows = new List<DataGridViewRow>();

    foreach (var student in students)
    {
        DataGridViewRow newRow = new DataGridViewRow();
        newRow.SetValues(new object[] { "test", "test", "test" });
        rows.Add(newRow);
    }

    dataGridView1.DataSource = rows;
}

So for testing purposes I did that bit up there. It's fetching the correct students, but the SetValues method doesn't seem to be doing what I think it's supposed to be doing.

How to elegantly tackle this Attendance Form problem?

At this point I'm open to suggestions. I've only done things this way because I'm just starting to tackle it. Maybe there's a better way using some Linq-fu.

The concrete question is, is this the right way to approach it, creating a collection of D开发者_StackOverflow中文版ataGridViewRows and setting them as the .DataSource?

Thanks for the suggestions.


You probably want something like this:

 var q = from i in new int[] { 1, 2, 3, 4, 5, 6, 7 }
                select new { Id = i, Name = "test" };
        dataGridView1.DataSource = q.ToList();

The trick is that the property names get translated to columns. You don't need to use the DataGridViewRow types here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜