如何优雅地解决这个考勤表问题?

发布于 2024-11-01 11:29:31 字数 1175 浏览 1 评论 0原文

https://i.sstatic.net/gWWg4.png

基本上,我想显示所有学生的列表(他们的名字位于最左侧),并在每行(每个学生)上显示组合框中所选月份的每个日期。

我有点困惑如何解决这个问题。

这是我到目前为止所得到的:

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

因此,出于测试目的,我在那里做了一些工作。它正在获取正确的学生,但 SetValues 方法似乎没有做我认为应该做的事情。

在此处输入图像描述

此时我愿意接受建议。我之所以这样做是因为我才刚刚开始解决这个问题。也许有更好的方法使用一些 Linq-fu。

具体问题是,创建 DataGridViewRows 集合并将它们设置为 .DataSource,这是处理它的正确方法吗?

感谢您的建议。

https://i.sstatic.net/gWWg4.png

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.

enter image description here

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 DataGridViewRows and setting them as the .DataSource?

Thanks for the suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2024-11-08 11:29:31

您可能想要这样的东西:

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

技巧是属性名称被转换为列。您不需要在此处使用 DataGridViewRow 类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文