如何从datatable将数据插入datagridview [英] how can I insert data into datagridview from datatable
本文介绍了如何从datatable将数据插入datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//DataTable dt = new DataTable();
db.Adapter("select distinct StudentName, Roll from StudentAttendance where Class = '" + comClass.Text.ToString() + "' and Shift ='" + cmShift.Text.ToString() + "' and Section = '" +cmSection.Text.ToString()+ "' and YearAt = '" + dateTimeYear.Text.ToString() + "'","student");
//MessageBox.Show(db.dataSet.Tables["student"].Rows.Count.ToString());
for (int i = 0; i < db.dataSet.Tables["student"].Rows.Count; i++)
{
grStudentAttend.Rows[i].Cells[0].Value = db.dataSet.Tables["student"].Rows[i][0].ToString();
grStudentAttend.Rows[i].Cells[1].Value = db.dataSet.Tables["student"].Rows[i][1].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
例外:
索引超出范围.必须为非负数并且小于集合的大小.
参数名称:index
Exception:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
推荐答案
为什么要这样做.请改用datasource属性.试试这个
Why are you doing this. use the datasource property instead. try this
grStudentAttend.DataSource = db.dataSet.Tables["student"];
grStudentAttend.DataBind();
这篇关于如何从datatable将数据插入datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文