
C#中索引超出范围。怎么解决 5
for(inti=0;i<list.Count;i++){MessageBox.Show(list[i].Student.StudentName);this.dataGr...
for (int i = 0; i < list.Count; i++)
{
MessageBox.Show(list[i].Student.StudentName);
this.dataGridView1.Rows[i].Cells["stName"].Value = list[i].Student.StudentName;
this.dataGridView1.Rows[i].Cells["suName"].Value = list[i].Subject.SubjectName; ;
this.dataGridView1.Rows[i].Cells["sResult"].Value = list[i].StudentResult;
this.dataGridView1.Rows[i].Cells["eDate"].Value = list[i].ExamDate;
} 展开
{
MessageBox.Show(list[i].Student.StudentName);
this.dataGridView1.Rows[i].Cells["stName"].Value = list[i].Student.StudentName;
this.dataGridView1.Rows[i].Cells["suName"].Value = list[i].Subject.SubjectName; ;
this.dataGridView1.Rows[i].Cells["sResult"].Value = list[i].StudentResult;
this.dataGridView1.Rows[i].Cells["eDate"].Value = list[i].ExamDate;
} 展开
5个回答
展开全部
C#中索引超出范围解决方法:
1,先判断:System.Windows.Forms.Label lb = new System.Windows.Forms.Label();//new一个Label控件
2,把下面这段程序加进去。
List<Label> lbl = new List<Label> ();
List<Label> labl = new List<Label>();
3,确认程序就可以了。
for (int i = 0; i < (a5 + 1); i++)
1,先判断:System.Windows.Forms.Label lb = new System.Windows.Forms.Label();//new一个Label控件
2,把下面这段程序加进去。
List<Label> lbl = new List<Label> ();
List<Label> labl = new List<Label>();
3,确认程序就可以了。
for (int i = 0; i < (a5 + 1); i++)
展开全部
首先要确定
你是想list中的信息替换网格的记录?
还是想将list中的信息插入到网格?
如果是替换,你需要检查一下list中的记录数是否与网格中的记录数一致(或者少于);
如果是插入,你的代码逻辑是错误的。
你是想list中的信息替换网格的记录?
还是想将list中的信息插入到网格?
如果是替换,你需要检查一下list中的记录数是否与网格中的记录数一致(或者少于);
如果是插入,你的代码逻辑是错误的。
追问
我要插入,那应该怎么写呢,跪求
追答
假设你的DataGrid的数据源是dataTable1
for (int i = 0; i < list.Count; i++)
{
MessageBox.Show(list[i].Student.StudentName);
DataRow newRow = dataTable1.NewRow();
newRow["stName"] = list[i].Student.StudentName;
newRow["suName"] = list[i].Subject.SubjectName; ;
newRow["sResult"] = list[i].StudentResult;
newRow["eDate"] = list[i].ExamDate;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先判断
if(list.Count>0)
{
for (int i = 0; i < list.Count; i++)
{
MessageBox.Show(list[i].Student.StudentName);
this.dataGridView1.Rows[i].Cells["stName"].Value = list[i].Student.StudentName;
this.dataGridView1.Rows[i].Cells["suName"].Value = list[i].Subject.SubjectName; ;
this.dataGridView1.Rows[i].Cells["sResult"].Value = list[i].StudentResult;
this.dataGridView1.Rows[i].Cells["eDate"].Value = list[i].ExamDate;
}
}
else{
MessageBox.Show("出错了");
}
if(list.Count>0)
{
for (int i = 0; i < list.Count; i++)
{
MessageBox.Show(list[i].Student.StudentName);
this.dataGridView1.Rows[i].Cells["stName"].Value = list[i].Student.StudentName;
this.dataGridView1.Rows[i].Cells["suName"].Value = list[i].Subject.SubjectName; ;
this.dataGridView1.Rows[i].Cells["sResult"].Value = list[i].StudentResult;
this.dataGridView1.Rows[i].Cells["eDate"].Value = list[i].ExamDate;
}
}
else{
MessageBox.Show("出错了");
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最有可能是dataGridView1里面row的行数和list的数量不对应,就是不相等,所以出现了所以超出范围。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dataGridView里面的Row的行数,小于List里面的元素个数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询