[英]Image display issue in datagridview from imagepath stored in database(sqlite) in windows form(C#)
I am working C# desktop application with sqlite. 我正在使用sqlite使用C#桌面应用程序。 Store Image path properly as
将图像路径正确存储为
like C:\\Users\\USER\\Pictures\\pms_1484911839.45213251.jpg
. 像
C:\\Users\\USER\\Pictures\\pms_1484911839.45213251.jpg
。
And I want to display the images in datagridview. 我想在datagridview中显示图像。 My Code
我的密码
private void loadTable(){
conn.Open();
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter("SELECT * FROM products", conn);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
foodListView.AutoGenerateColumns = false;
foodListView.AllowUserToAddRows = false;
foodListView.RowHeadersVisible = false;
foodListView.DataSource = dt;
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
Image img;
int i = 0;
imageColumn.HeaderText = "Image";
imageColumn.Name = "image";
foodListView.Columns.Insert(3, imageColumn);
foreach (DataRow dr in dt.Rows)
{
img = Image.FromFile(@dr["image"].ToString());
foodListView.Rows[i].Cells["image"].Value = img;
i++;
}
conn.Close();}
My Output is like 我的输出就像
Where is my problem. 我的问题在哪里? Thank you.
谢谢。
//foreach (DataRow dr in dt.Rows)
//{
// img = Image.FromFile(@dr["image"].ToString());
// foodListView.Rows[i].Cells["image"].Value = img;
// i++;
//}
foodListView.CellFormatting += foodListView_CellFormatting;
private void foodListView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (foodListView.Columns[e.ColumnIndex].Name == "image")
{
e.Value = Bitmap.FromFile(e.Value.ToString());
e.FormattingApplied = true;
}
}
try to apply cellformatting. 尝试应用单元格格式。 make sure column is mapped with respective field.
确保列已与相应字段映射。
If you want to load image from aa file into the DataGridView
(in your case the path of the file is in the database) , then here's a sample code : 如果要将图像从文件加载到
DataGridView
(在您的情况下,文件的路径在数据库中),那么下面是示例代码:
Bitmap img;
img = new Bitmap(dr[3].ToString); ////change column index as required
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
////other codes here
dgv.Rows[0].Cells[3].Value = img;
Hope this helps :) 希望这可以帮助 :)
Actually the code in the above working absolutely fine. 其实上面的代码工作正常。 But the problem was I call
loadTable()
before the form loaded. 但是问题是我在加载表单之前先调用
loadTable()
。 That's why the function creates image dispay issue. 这就是为什么该函数会产生图像支付问题的原因。 When I call the function after the form load images load properly.
当窗体加载图像正确加载后,我调用该函数时。 Thank you everyone.
谢谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.