We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Image Alignment in a Cell

Hi again - We're trying to center an image in a cell - in the PrepareViewStyleInfo event we're setting the celltype to ImageCell, which works, but calling e.Style.HorizontalAlignment = GridHorizontalAlignment.Center fails to center the image - is there another way? Thanks Ivan Pelly

8 Replies

AD Administrator Syncfusion Team November 19, 2002 12:23 AM UTC

The OnDraw routine in the derived cellrenderer just draws the bitmap to fit the cell. If you want to center it, then you will have to modifiy the code to center it. Here is a try at it.
protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style)
	if (clientRectangle.IsEmpty)
	// CellLayout layout = GetCellLayout(int rowIndex, int colIndex, GridStyleInfo style)

	if(style.Tag is Bitmap)
		int cellWidth = clientRectangle.Width;
		int cellHeight = clientRectangle.Height;
		int bmpWidth = ((Bitmap) style.Tag).Width;
		int bmpHeight = ((Bitmap) style.Tag).Height;
		int left = clientRectangle.X + (cellWidth - bmpWidth) / 2;
		int top = clientRectangle.Y + (cellHeight - bmpHeight) / 2;
		g.DrawImage((Bitmap) style.Tag, left, top, bmpWidth, bmpHeight);

IP Ivan Pelly November 19, 2002 02:54 PM UTC

I think I'm missing a piece of the puzzle - does the above mean that we need to subclass the cell renderer and use this subclassed version in the grid? Thanks - Ivan Pelly

AD Administrator Syncfusion Team November 19, 2002 04:16 PM UTC

If you are setting the CellType to the "ImageCell" that is part of the EcelSelectionMarker sample, then you should have added a CellModel class and a CellRenderer class directly to your project. (That sample includes these classes in an ImageGridCell.cs file or its 2 vb equivalent files.) I was suggesting that you replace the ImageCellRenderer.OnDraw override with the code above. Since you are including the code directly in your project, there is no need to override anything, you can just replace the method. If you are using some other ImageCell control, then can you explain what you are doing?

IP Ivan Pelly November 19, 2002 05:11 PM UTC

We're simply calling e.Style.CellType = "ImageCell" in the PrepareViewStyleInfo event. Do we need to do it differently?

AD Administrator Syncfusion Team November 19, 2002 09:44 PM UTC

Yes, you'll need to do more work. "ImageCell" is not one of the built-in celltypes like "CheckBox" and "ComboBox". What you are using is the standard "Static" celltype. With this static cell type, you set an ImageList and and ImageIndex, and the cell control will draw the image for you. But it does not try to position the image, but instead leaves room for text to appear along with the image. If you want more control over how the image is drawn, then you can use the technique that is illustrated in the ExcelSelectionMarker sample that ships with version 1.5. In that sample, there is a file that contains another celltype control. You would copy this file (or files for VB) into your project so you could use this celltype in your project. You would use the code from the sample's Form Load event (CellModels.Add) to register this new celltype so you can use it in your grid, and to load your bitmap into the style's Tag member. Once you do this, you can set the ImageCell (if that is the name your use in the CellModel.Add method) celltype to see your bitmap drawn. But, the basic behavior of this control is to fill the cell rect with the bitmap. If you want to center it, then you will have to swap out the draw code I mentioned before.

IP Ivan Pelly November 20, 2002 02:56 PM UTC

I got my code working as in the example above - the problem now is that the image is stretched to fit the entire cell - I just want it at its native size in the middle of the cell. Is there an easy fix? Thanks

AD Administrator Syncfusion Team November 20, 2002 03:32 PM UTC

If you swap out the OnDraw code that is part of the ImageCell from the ExcelSelectionMarker sample with the code that I posted in th esecond entry above, I think the image should be drawn centered with its original size. Have you tried this, and it did not work?

IP Ivan Pelly November 20, 2002 07:08 PM UTC

It works! Thanks.

Live Chat Icon For mobile
Up arrow icon