Click here to Skip to main content
15,914,413 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#

byte[] bimage = (byte[])(new ImageConverter().ConvertTo(dataGridView1[i, j].Value, typeof(byte[])));



//all the rows show no error when putting the picture but if I do not put a picture of the ballast, it gives an error.

What I have tried:

C#

private void button5_Click(object sender, EventArgs e)
{
{
Microsoft.Office.Interop.Excel.Application uyg = new Microsoft.Office.Interop.Excel.Application();
uyg.Visible = true;
Microsoft.Office.Interop.Excel.Workbook kitap = uyg.Workbooks.Add(System.Reflection.Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)kitap.Sheets[1];
Microsoft.Office.Interop.Excel.Range myRange;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[1, i + 1];
myRange.Value2 = dataGridView1.Columns[i].HeaderText;
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[j + 2, i + 1];

if (dataGridView1[i, j].GetType() == typeof(DataGridViewImageCell))
{
if (dataGridView1[i, j].Value != null)
{
byte[] bimage = (byte[])(new ImageConverter().ConvertTo(dataGridView1[i, j].Value, typeof(byte[])));

MemoryStream ms = new MemoryStream(bimage);
Image image = Image.FromStream(ms);
image.Save(@"D:\dosya.jpg");
float Left = (float)(myRange.Left);
float Top = (float)(myRange.Top);
const float ImageSize = 32;
sheet1.Shapes.AddPicture(@"D:\dosya.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
myRange.RowHeight = ImageSize + 2;
File.Delete(@"D:\dosya.jpg");

}
else {
myRange.Value2 = dataGridView1[i, j].Value;}
}
}
}
}
}
Posted
Updated 13-Aug-18 6:22am
v2
Comments
Codingnow20 13-Aug-18 16:23pm    
What is the error ur getting??

1 solution

You already posted this at I get an error when I don't put any pictures on the line.[^]. And you are still not telling us what the error is.
 
Share this answer
 
Comments
Member 13947049 14-Aug-18 1:24am    
this code works when I put the picture in all the lines. I do not want to give an error in this case.the whole column does not give an error when it is a picture but if 1 line in that column is empty, it gives an error. I need to put the line in the column in the empty place instead of the picture.

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