Click here to Skip to main content
15,358,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to display the images from Project folder(solution explorer) to datagridviewimage column cell.

i got the following error:


An exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll but was not handled in user code

Additional information: ~/images/pdf.png


i stored the images in solution explorer
(images\pdf.png)

What I have tried:

void gvProduct_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
       {

           if (e.RowIndex > -1 && e.ColumnIndex == this.gvProduct.Columns["Calibrateimg"].Index)
           {
               if (this.gvProduct["CalibratCertificate", e.RowIndex].Value != null)
               {
                  // string s = this.gvProduct["CalibrationCertificate", e.RowIndex].Value.ToString();

                           string Calibaration = gvProduct.Rows[e.RowIndex].Cells["CalibratCertificate"].Value.ToString().Trim();
                           string targetPath = System.Windows.Forms.Application.StartupPath + "\\App_Data\\CalibrationCertificate\\";

                           string extn = Path.GetExtension(Calibaration);

                           // read the file data
                           switch (extn)
                           {
                               case ".pdf":
                            MessageBox.Show("test img");
                           e.Value = Image.FromFile("~/images/pdf.png");
                                   break;

                               case ".docs":
                                   e.Value = Image.FromFile("~/images/word.png");
                                   break;
                            }
               }

           }
       }


<pre> public void BindGrid()
        {
            DataSet ds = new DataSet();
            ds = BindData("BindProduct", this.branch, string.Empty);


            DataGridViewImageColumn img = new DataGridViewImageColumn();
            img.Name = "Calibrateimg";
            img.HeaderText = "Calibrateimg";

            if (ds.Tables[0].Rows.Count > 0)
            {
                gvProduct.AutoGenerateColumns = true;
                gvProduct.DataSource = ds.Tables[0];
                
                gvProduct.DataBindings.ToString();


                this.gvProduct.Columns.Add(img);
                this.gvProduct.Columns["CalibratCertificate"].Visible = false;

                this.gvProduct.CellFormatting += new DataGridViewCellFormattingEventHandler(gvProduct_CellFormatting);


                gvProduct.Refresh();
             
                
                
            }
Posted
Updated 11-Apr-22 0:25am

1 solution

If you're including it as part of your project/solution and want it deployed with your executable then you probably want to look at using embedded resources. At compile-time these will get embedded within your exe and remain accessible to the application at runtime.

This StackOverflow answer explains it[^] quite well, you just need to change the images "Build Type" to embedded resource, and then you can access those resources programmatically.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900