如何优雅地解决这个考勤表问题?
基本上,我想显示所有学生的列表(他们的名字位于最左侧),并在每行(每个学生)上显示组合框中所选月份的每个日期。
我有点困惑如何解决这个问题。
这是我到目前为止所得到的:
//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,这是处理它的正确方法吗?
感谢您的建议。
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要这样的东西:
技巧是属性名称被转换为列。您不需要在此处使用 DataGridViewRow 类型。
You probably want something like this:
The trick is that the property names get translated to columns. You don't need to use the DataGridViewRow types here.