The cell border disappears.

I have two questions.

1.

I added the image to the cell with reference to the following.

https://help.syncfusion.com/windowsforms/datagrid/conditionalstyling#adding-an-image-for-a-cell




As you can see, the border disappears.

The code was written like this:

private void sfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e)
        {
            if (e.RowIndex > 1)
            {
                if (e.Column.MappingName == "message" && e.DisplayText != "")
                {
                     if (File.Exists(e.DisplayText))
                     {
                         e.Handled = true;
                         Image img = Image.FromFile(e.DisplayText);
                         float percent = e.Bounds.Height / (float)img.Height;
                         float imgHeight = img.Height * percent - 10;
                         float imgwidth = img.Width * percent - 10;

                         e.Graphics.DrawImage(img, e.Bounds.X + 5, e.Bounds.Y + 5, imgwidth, imgHeight);
                         ((SfDataGrid)e.OriginalSender).TableControl.RowHeights[e.RowIndex] = 180;
                         ((SfDataGrid)e.OriginalSender).BackColor = Color.FromArgb(38, 38, 38);
                     }
                }
            }
        }

What is the problem?

2.

I want to export this datagrid to excel.

But the picture is not exported.
How can I export even pictures to excel?

Like the captured picture, the column is a mixture of picture and text.

11 Replies 1 reply marked as answer

MA Mohanram Anbukkarasu Syncfusion Team June 9, 2021 06:37 AM UTC

Hi nam ki hun, 

Thanks for contacting Syncfusion support.  

Query 
Solution 
As you can see, the border disappears. 

What is the problem? 
If you handled the DrawCell event, borders will not be drawn for the cell.  
In the help documentation you can see that we have included code to draw the borders using Graphics.DrawLine method. You have to included those line of codes to resolve this.  

this.sfDataGrid1.DrawCell += SfDataGrid1_DrawCell; 
private void SfDataGrid1_DrawCell(object sender, DrawCellEventArgs e) 
    if (e.RowIndex == 1 && e.Column.MappingName == "ShipCountry") 
    { 
        e.Handled = true; 
        e.Graphics.DrawImage(Image.FromFile(@"../../US.jpg"), e.Bounds.X + 20, e.Bounds.Y); 
        Pen borderPen = new Pen(Color.LightGray); 
        e.Graphics.DrawLine(borderPen, e.Bounds.Right, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom); 
        e.Graphics.DrawLine(borderPen, e.Bounds.Left, e.Bounds.Bottom, e.Bounds.Right, e.Bounds.Bottom); 
    } 


I want to export this datagrid to excel. 

But the picture is not exported. 
How can I export even pictures to excel? 
While exporting the DataGrid to excel only the values which are residing in the underlying DataSource will be exported. If you need to export with the image, you need to manually add the images to the excel by using ExcelExportingOptions.CellExporting event. 

We have prepared a sample for this and it is available in the following link for your reference.  






Please let us know if you require any other assistance from us. 

Regards, 
Mohanram A. 


Marked as answer

NK nam ki hun June 10, 2021 07:09 AM UTC

Thanks to you, the border issue has been fixed.

However, referring to your second answer, I modified my code, and two problems occurred.

1.

Pictures are exported to excel file.



However, like the capture file, they overlap because of the size of the picture.
So I tried like this to fix the rowheight.

e.Range.Cells[0].RowHeight = 50.0;
e.Range.RowHeight = 50.0;

But it doesn't apply.
How can I increase the row height?

2.

If there are many pictures, out of memory problem occurs.
Is there any way to solve the out of memory problem?


MA Mohanram Anbukkarasu Syncfusion Team June 11, 2021 12:31 PM UTC

Hi nam ki hun, 

Thanks for the update. 

How can I increase the row height? 


We are currently working on this. We will check and update with details on June 15, 2021. We appreciate your patience until then.  
If there are many pictures, out of memory problem occurs. 
Is there any way to solve the out of memory problem? 
We are unclear with this query. Please revert to us with more detail about this query and the problem you are facing when using more pictures. It will be more helpful for us to understand the requirement and to provide a prompt solution. 

Regards, 
Mohanram A. 



MA Mohanram Anbukkarasu Syncfusion Team June 15, 2021 01:43 PM UTC

Hi nam ki hun, 

We regret for the inconvenience.  

Please revert to us with the details we have requested in our previous update. Also please confirm whether you need to change the row height for all the rows in the excel sheet or only for some specific rows. Please confirm these details. It will be more helpful for us to understand the requirement and to provide a prompt solution. 

Regards, 
Mohanram A. 



NK nam ki hun June 22, 2021 04:56 AM UTC

Thanks for your reply.

I want to set the picture of different sizes to the same size and reset the height of only the rows with pictures.


I look forward to your reply.


Regards,

Ki hun



MA Mohanram Anbukkarasu Syncfusion Team June 23, 2021 12:53 PM UTC

Hi nam ki hun, 

Thanks for the update.  

Please refer the below given KB documentation for changing the row height for the worksheet.  

KB references :  

Regards, 
Mohanram A. 




NK nam ki hun June 24, 2021 04:33 AM UTC

I can't solving the memory problem.

If sfdatagrid has a lot of pictures or data, it will run out of memory.


This is my code ( This code is running inside CellExporting. )


if (File.Exists(imgPath))

{

using (Image image = Image.FromFile(imgPath))

{

e.Range.Worksheet.Pictures.AddPicture(e.Range.Row, e.Range.Column, image, 100, 100); <- Out of memory point.

e.Handled = true;

}

}






I think that Image-related byte[] has a memory leak in AddPicture().

maybe not...


Please check it.




MA Mohanram Anbukkarasu Syncfusion Team June 25, 2021 11:39 AM UTC

Hi nam ki hun,  

Thanks for the update. 

It seems the reported issue is related XlsIO. We have forwarded this to the corresponding team for analysis. We will update with further details on June 29, 2021. We appreciate your patience and understanding.   

Regards,  
Mohanram A 



MA Mohanram Anbukkarasu Syncfusion Team June 29, 2021 12:48 PM UTC

Hi nam ki hun, 

Thanks for your patience.  

Please find the below given details provided by XlsIO team. 

When images are being added in Excel using XlsIO, memory gets hold in stack. When the number of pictures being added is high, OutOfMemoryException is thrown. We suggest you to use image with lower file sizes when the number of records are more. 

Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 



NK nam ki hun July 7, 2021 01:23 AM UTC

Thanks for checking.


So there is no solution?



MA Mohanram Anbukkarasu Syncfusion Team July 7, 2021 09:24 AM UTC

Hi nam ki hun,   

We regret to let you know that as per the details shared by XlsIO team, using lower size files are the only solution for this. Please let us know if you have any other concerns.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon