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