dataGridView中的OutOfMemory异常

时间:2012-12-18 11:07:48

标签: c#-4.0 datagridview

我正在使用2 datagridview来查看Windows窗体应用程序中的数据。

第一个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不会在第一次点击时出现,但是在DGV1的VIEW列上点击5-8之后出现。 我怎样才能清理记忆?

1 个答案:

答案 0 :(得分:3)

尝试手动处理和创建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;