我在dataGridView中有14列但是我想从数据库中的费用类型的名称填充dataGridView的第一列。但它给了我ArgumentOutOfRangeException。这是我的代码。
myDatabase dbOperation = new myDatabase();
DataTable table = new DataTable();
table = dbOperation.select("name from feetypes");
for (int i = 0; i < table.Rows.Count; i++)
{
try
{
dataGridView1.Rows[i].Cells["feeType"].Value = table.Rows[i]["name"].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
答案 0 :(得分:1)
问题:您试图访问Rows
而不创建它们。
解决方案:您需要在运行时将行添加到DataGridView
。
试试这个:
for (int i = 0; i < table.Rows.Count; i++)
{
try
{
dataGridView1.Rows[i].Cells["feeType"].Value = table.Rows[i]["name"].ToString();
dataGridView1.Rows.Add(); //add this
}