C# 从数据库填充dataGridView中的特定单元格

C# 从数据库填充dataGridView中的特定单元格,c#,winforms,datagridview,C#,Winforms,Datagridview,我在dataGridView中有14列,但我想从数据库中费用类型的名称填充dataGridView的第一列。但这给了我一个例外。这是我的密码 myDatabase dbOperation = new myDatabase(); DataTable table = new DataTable(); table = dbOperation.select("name from feetypes"); for (int i = 0; i < table.Rows.Co

我在dataGridView中有14列,但我想从数据库中费用类型的名称填充dataGridView的第一列。但这给了我一个例外。这是我的密码

 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());
                }
            }
myDatabase dbOperation=newmydatabase();
DataTable=新的DataTable();
table=dbOperation.select(“feetypes中的名称”);
for(int i=0;i
问题:您试图访问
行而不创建它们


解决方案:您需要在运行时将行添加到
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
   }
for(int i=0;i
数据库中有多少行,如果大于14,则可能会得到ArgumentOutOfRangeException。在查询数据库中的第一个填充时,是否可以从数据库进行连接?行数少于14。需要在运行时将行添加到GridView。在循环中添加此->
dataGridView1.rows.add()
不客气,亲爱的:)很高兴能帮助你。@忠诚:请不要忘记接受它作为回答:)