0

I am working C# desktop application with sqlite. Store Image path properly as

like C:\Users\USER\Pictures\pms_1484911839.45213251.jpg.

And I want to display the images in 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

enter image description here

Where is my problem. Thank you.

10
  • When you debug, what is the value of @dr["image"].ToString() for any given record which isn't being displayed properly? What is the resulting img? I'd also recommend declaring the img variable inside the loop so you're not potentially over-writing all previous images with each iteration of the loop. It's possible that only the last image is broken, but is also overwriting the rest of them. Commented Apr 23, 2018 at 14:35
  • When I debug I got C:\Users\USER\Pictures\pms_1484911839.45213251.jpg which is exists. I also declare img inside the loop. No change happen. Commented Apr 23, 2018 at 14:42
  • is the image saved as the path only in the database ? Commented Apr 23, 2018 at 14:48
  • @IftakharulAlam , cheers for being from my country :) Commented Apr 23, 2018 at 14:49
  • Yes. Like exactly same as C:\Users\USER\Pictures\pms_1484911839.45213251.jpg Commented Apr 23, 2018 at 14:50

3 Answers 3

2
//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.

Sign up to request clarification or add additional context in comments.

5 Comments

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Windows.Forms.ConvertEventArgs.Value.get returned null. on line e.Value = Bitmap.FromFile(e.Value.ToString());
imageColumn.DataPropertyName = "image"; you have to mapp the colum first
Working. Thank you. I think this is more optimize solution.
But If I recall the function again it shows error System.IO.FileNotFoundException: 'System.Drawing.Bitmap' in the line e.Value = Bitmap.FromFile(e.Value.ToString());. Any Idea why?
@IftakharulAlam check the file path. also foodListView.CellFormatting -= foodListView_CellFormatting; unsubscribe event before subscribe it.
0

If you want to load image from a a file into the DataGridView(in your case the path of the file is in the database) , then here's a sample code :

  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 :)

7 Comments

Here i am simply using the Bitmap class ... and the op's code makes very less sense as he's,on the first hand,binding the datagridview to the datatable and then again looping through the datatable to add images to the dgv..i wonder why he didn't choose to do it all in one way
this will not make any difference at all. The images are not in the dbms, so he needs to fill in the images after binding.
@zackraiyan your solution isn't working. It says In valid parameter in img = new Bitmap(dr[3].ToString())
What will be the correct format? That's what I want to know.
Do u really wanna go with a datatable first ? and iterate over it twice ? there are better solutions available :)
|
0

Actually the code in the above working absolutely fine. But the problem was I call loadTable() before the form loaded. That's why the function creates image dispay issue. When I call the function after the form load images load properly. Thank you everyone.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.