【问题标题】:How to elegantly tackle this Attendance Form problem?如何优雅地解决这个考勤表问题?
【发布时间】:2011-08-05 15:02:46
【问题描述】:

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

我有点不知道如何解决这个问题。

这是我目前所拥有的:

//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 是否正确?

感谢您的建议。

【问题讨论】:

    标签: c# winforms linq collections datagridview


    【解决方案1】:

    你可能想要这样的东西:

     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 类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 2010-10-04
      • 2023-02-05
      • 2014-10-07
      • 1970-01-01
      相关资源
      最近更新 更多