简体   繁体   English

来自Windows窗体(C#)中存储在数据库(sqlite)中的图像路径的datagridview中的图像显示问题

[英]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.

相关问题 以Windows形式从存储在数据库中的imagepath在gridview中显示图像 - display images in gridview from imagepath stored in database in windows form 使用C#中的imagepath从Oracle数据库检索图像 - To retrieve image from oracle database by using imagepath in c# 将图像字节类型从数据库显示到DataGridview C#中 - Display image byte type from database into DataGridview C# 在C#中的datagridview中选择记录时,如何在图片框中显示存储在数据库表中的图像 - How to display image stored in a database table in a picturebox when selecting a record in datagridview in c# c#在datagridview中显示数据库中的数据 - c# Display data from database in datagridview 从SQL用C#在DataGridView中显示图像 - Display an image in DataGridView in C# from SQL 无法在C#Windows Form Appliction中将数据库中的图像显示到Crystal Report上 - Unable to display image from database onto Crystal Report in C# Windows Form Appliction 在 datagridview 组合框中选择在另一个单元格中显示图像 c# windows 形式 - selection in datagridview comboboxcolumn display image in another cell c# windows form 从数据库到DataGridView的C#图像 - C# Image from Database to DataGridView C#Windows窗体Datagridview单元格显示文本 - c# windows form datagridview cell display text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM