簡體   English   中英

從數據庫填充dataGridView中的特定單元格

[英]populating a specific cell in dataGridView from database

我在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());
                }
            }

問題:您試圖訪問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
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM