2012-12-18 85 views
1

我正在使用2 datagridview來查看Windows窗體應用程序中的數據。dataGridView中OutOfMemory異常

第一臺DGV根據傳遞給他們的ID顯示產品。

單擊DGV1中的我的一列的VIEW,它將產品ID傳遞到數據庫並從數據庫獲取完整記錄,並將記錄顯示到其他DGV2。

這是我的代碼:現在

if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == (Object)"View") 
       { 
        if (con.State == ConnectionState.Closed) 
        { 
         con.Open(); 
        } 

        int prod_id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value); 
        dataGridView2.DataSource = null; 
        dataGridView2.Rows.Clear(); 

        retFindProducts = new MySqlCommand("SELECT DISTINCT tf_history.thefind_id, tf_product.product_id, tf_product.`name`, tf_product.product_url, tf_product.image_tpm, tf_product.image_thefind, tf_product.image_accuracy, (SELECT MIN(tf_h.price) FROM tf_history AS tf_h WHERE tf_h.thefind_id = tf_history.thefind_id) as price, oc_product.price AS priceTPM FROM tf_product LEFT JOIN tf_history ON tf_product.product_id = tf_history.product_id AND tf_product.thefind_id = tf_history.thefind_id LEFT JOIN oc_product ON tf_product.product_id = oc_product.product_id WHERE tf_product.product_id = @product_id", con); 
        historyData = new MySqlCommand("SELECT price, date from tf_history WHERE thefind_id = @thefind_id", con); 

        retFindProducts.CommandTimeout = 300; 
        historyData.CommandTimeout = 300; 

        retFindProducts.Parameters.AddWithValue("@product_id", prod_id); 
        dr = retFindProducts.ExecuteReader(); 
        retFindProducts.Parameters.Clear(); 

        while (dr.Read()) 
        { 
         dataGridView2.Rows.Add(); 
         long fI = Convert.ToInt64(dr["thefind_id"]); 
         //if (!findId.Exists(p => p.Item1 == fI)) 
         findId.Add(new Tuple<long>(fI)); 
         decimal findPrice = Convert.ToDecimal(dr["price"]); 
         decimal tpmPrice = Convert.ToDecimal(dr["priceTPM"]); 

         if (findPrice > tpmPrice) 
         { 
          dataGridView2.Rows[cnt].Cells[4].Style.ForeColor = Color.Green; 
          dataGridView2.Rows[cnt].Cells[4].Style.Font = new Font(dataGridView2.DefaultCellStyle.Font.FontFamily, 9, FontStyle.Regular); 
         } 
         else if (findPrice < tpmPrice) 
         { 
          dataGridView2.Rows[cnt].Cells[4].Style.ForeColor = Color.Red; 
          dataGridView2.Rows[cnt].Cells[4].Style.Font = new Font(dataGridView2.DefaultCellStyle.Font.FontFamily, 10, FontStyle.Bold); 
         } 

         dataGridView2.Rows[cnt].Cells[0].Value = Image.FromFile(dr["image_tpm"].ToString()); 
         dataGridView2.Rows[cnt].Cells[1].Value = Image.FromFile(dr["image_thefind"].ToString()); 
         dataGridView2.Rows[cnt].Cells[2].Value = dr["name"].ToString(); 
         dataGridView2.Rows[cnt].Cells[3].Value = dr["product_url"].ToString(); 
         dataGridView2.Rows[cnt].Cells[4].Value = dr["price"].ToString(); 
         dataGridView2.Rows[cnt].Cells[5].Value = dr["image_accuracy"].ToString(); 

         cnt++; 
        } 

        foreach (DataGridViewRow row in dataGridView2.Rows) 
        { 
         row.Height = 60; 
        } 
        dr.Close(); 
       } 

,該outofexception不會亮起點擊的第一次,但說到後5-8點擊次數DGV1的視圖列。 我該如何清理內存?

回答

2

嘗試手動處置和創建datagridview。代碼可以是這樣的:

  dgv.Dispose(); 
      dgv = new DataGridView(); 
      DataGridViewColumn col1 = new DataGridViewTextBoxColumn(); 
      DataGridViewColumn col2 = new DataGridViewTextBoxColumn(); 
      DataGridViewColumn col3 = new DataGridViewTextBoxColumn(); 
      DataGridViewColumn col4 = new DataGridViewTextBoxColumn(); 
      DataGridViewColumn col5 = new DataGridViewTextBoxColumn(); 

      col1.HeaderText = "COl1"; 
      col2.HeaderText = "COl2"; 
      col3.HeaderText = "COl3"; 
      col4.HeaderText = "COl4"; 
      col5.HeaderText = "COl5"; 
      dgv = new System.Windows.Forms.DataGridView(); 
      dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
      dgv.Location = new System.Drawing.Point(59, 101); 
      dgv.Name = "dataGridView1"; 
      dgv.Size = new System.Drawing.Size(240, 150); 
      dgv.TabIndex = 2; 

      dgv.Columns.Add(col1); 
      dgv.Columns.Add(col2); 
      dgv.Columns.Add(col3); 
      dgv.Columns.Add(col4); 
      dgv.Columns.Add(col5); 

     this.Controls.Add(dgv); 

     dgv.ColumnCount = 5; 

     dgv.Visible = true; 
+1

工作..謝謝! – Paz