Category / Section
How to display multiple images in a cell in WinForms GridControl?
1 min read
Display multiple images in a grid cell
To display multiple Images in a cell, you need to handle the CellDrawn Event. In the following code example, the image is attained from the ImageList that has the collection of images. In the CellDrawn event handler, the images are drawn in the cell by using the bounds of the rectangle.
C#
//Sets the cellType as Image this.gridControl1[3, 3].CellType = GridCellTypeName.Image; Bitmap bmp1 = SystemIcons.Exclamation.ToBitmap(); Bitmap bmp2 = SystemIcons.Question.ToBitmap(); Bitmap bmp3 = SystemIcons.Shield.ToBitmap(); //Adds the bitmaps to the image list images.Images.Add(bmp1); images.Images.Add(bmp2); images.Images.Add(bmp3); //Hooks the CellDrawn event in Form_Load to add images this.gridControl1.CellDrawn += gridControl1_CellDrawn; void gridControl1_CellDrawn(object sender, GridDrawCellEventArgs e) { if (e.RowIndex ==3 && e.ColIndex==3 ) { Rectangle rect = new Rectangle(e.Bounds.Location, images.ImageSize); rect.Intersect(e.Bounds); for (int i = 0; i < this.images.Images.Count; ++i) { GridStaticCellRenderer.DrawImage(e.Graphics, this.images, i, rect, false); rect.Offset(this.images.ImageSize.Width + 1, 0); } } }
VB
'Sets the cellType as Image Private Me.gridControl1(3, 3).CellType = GridCellTypeName.Image Private bmp1 As Bitmap = SystemIcons.Exclamation.ToBitmap() Private bmp2 As Bitmap = SystemIcons.Question.ToBitmap() Private bmp3 As Bitmap = SystemIcons.Shield.ToBitmap() 'Adds the bitmaps to the image list images.Images.Add(bmp1) images.Images.Add(bmp2) images.Images.Add(bmp3) 'Hooks the CellDrawn event in Form_Load to add images Me.gridControl1.CellDrawn += gridControl1_CellDrawn Private Sub gridControl1_CellDrawn(ByVal sender As Object, ByVal e As GridDrawCellEventArgs) If e.RowIndex =3 AndAlso e.ColIndex=3 Then Dim rect As New Rectangle(e.Bounds.Location, images.ImageSize) rect.Intersect(e.Bounds) For i As Integer = 0 To Me.images.Images.Count - 1 GridStaticCellRenderer.DrawImage(e.Graphics, Me.images, i, rect, False) rect.Offset(Me.images.ImageSize.Width + 1, 0) Next i End If End Sub
Figure 1: GridControl with multiple images in a cell.
Samples:
C#: MultipleImages
VB: MultipleImages