1

我正在使用 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 次后出现。我怎样才能清除内存?

4

1 回答 1

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;
于 2012-12-18T13:23:41.163 回答