Articles in this section
Category / Section

How to add an image to the Pushbutton cell?

2 mins read

Description

You can add an image to the PushButtonCell. To achieve this, create a new CellModel and CellRenderer.

Solution

In the following code example, GridPushButtonCellModel, GridPushButtonCellRenderer are created by inheriting GridPushButtonCellModel and GridPushButtonCellRenderer from sample. Refer to the following code example to know how to use these classes.

C#

//CellModel
public class GridPushButtonImageCellModel: GridPushButtonCellModel
{
protected GridPushButtonImageCellModel(SerializationInfo info, StreamingContext context)
: base(info, context){}
public GridPushButtonImageCellModel(GridModel grid)
: base(grid){}
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
return new GridPushButtonImageCellRenderer(control, this);
}
}
//CellRenderer
public class GridPushButtonImageCellRenderer : GridPushButtonCellRenderer
{
private ImageCellButton pushButton;
public GridPushButtonImageCellRenderer(GridControlBase grid, GridPushButtonImageCellModel cellModel)
: base(grid, cellModel)
{
base.RemoveButton(base.GetButton(0));
AddButton(pushButton = new ImageCellButton(this));
}
}
//ImageCellButton
public class ImageCellButton : GridCellButton
{
static GridIconPaint iconPainter;
static ImageCellButton()
{
iconPainter = new GridIconPaint("ImagePushButton.", typeof(ImageCellButton).Assembly);
}
public ImageCellButton(GridPushButtonCellRenderer control)
: base(control)
{
Console.WriteLine("In ImageCellButton");
}
public override void Draw(Graphics g, int rowIndex, int colIndex, bool bActive,
GridStyleInfo style)
{
base.Draw(g, rowIndex, colIndex, bActive, style);
// draw the button
bool hovering = IsHovering(rowIndex, colIndex);
bool mouseDown = IsMouseDown(rowIndex, colIndex);
bool disabled = !style.Clickable;
ButtonState buttonState = ButtonState.Normal;
if (disabled)
buttonState |= ButtonState.Inactive|ButtonState.Flat;
else if (!hovering && !mouseDown)
buttonState |= ButtonState.Flat;
Point ptOffset = Point.Empty;
if (mouseDown)
{
ptOffset = new Point(1, 1);
buttonState |= ButtonState.Pushed;
}
DrawButton(g, Bounds, buttonState, style);
Image img = Image.FromFile(Application.StartupPath + "..\\..\\..\\Browse.bmp");
Bitmap bmp = img as Bitmap;
Rectangle r = iconPainter.PaintIcon(g, Bounds, ptOffset, bmp, Color.Black);
}
}

VB

'CellModel
Public Class GridPushButtonImageCellModel
Inherits GridPushButtonCellModel
Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
MyBase.New(info, context)
End Sub
Public Sub New(ByVal grid As GridModel)
MyBase.New(grid)
End Sub
Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase
Return New GridPushButtonImageCellRenderer(control, Me)
End Function
End Class
'CellRenderer
Public Class GridPushButtonImageCellRenderer
Inherits GridPushButtonCellRenderer
Private pushButton = New ImageCellButton(Me)
Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridPushButtonImageCellModel)
MyBase.New(grid, cellModel)
MyBase.RemoveButton(MyBase.GetButton(0))
AddButton(pushButton)
End Sub
End Class
'ImageButton
Public Class ImageCellButton
Inherits GridCellButton
Private Shared iconPainter As GridIconPaint
Shared Sub New()
iconPainter = New GridIconPaint("ImagePushButton.", GetType(ImageCellButton).Assembly)
End Sub
Public Sub New(ByVal control As GridPushButtonCellRenderer)
MyBase.New(control)
Console.WriteLine("In ImageCellButton")
End Sub
Public Overrides Sub Draw(ByVal g As Graphics, ByVal rowIndex As Integer, ByVal colIndex As Integer, ByVal bActive As Boolean, ByVal style As GridStyleInfo)
MyBase.Draw(g, rowIndex, colIndex, bActive, style)
' draw the button
Dim hovering As Boolean = IsHovering(rowIndex, colIndex)
Dim mouseDown As Boolean = IsMouseDown(rowIndex, colIndex)
Dim disabled As Boolean = Not style.Clickable
Dim buttonState As ButtonState = buttonState.Normal
If disabled Then
buttonState = buttonState Or buttonState.Inactive Or buttonState.Flat
ElseIf (Not hovering) AndAlso (Not mouseDown) Then
Dim ptOffset As Point = Point.Empty
If mouseDown Then
Dim img As Image = Image.FromFile(Application.StartupPath & "..\..\..\Browse.bmp")
Dim bmp As Bitmap = TryCast(img, Bitmap)
Dim r As Rectangle = iconPainter.PaintIcon(g, Bounds, ptOffset, bmp, Color.Black)
End Sub
End Class

Adding CellModel

The following code example illustrates how to add cell model to the GridDataBoundGrid. The text “ImagePushButton” mentioned while adding the CellModel is used as CellType.

C#

//Adding the ImagePushButton Model in the GridDataBoundGrid Model.
this.gridDataBoundGrid1.Model.CellModels.Add("ImagePushButton", new GridPushButtonImageCellModel(this.gridDataBoundGrid1.Model));

VB

'Adding the ImagePushButton Model in the GridDataBoundGrid Model.
Me.gridDataBoundGrid1.Model.CellModels.Add("ImagePushButton", New GridPushButtonImageCellModel(Me.gridDataBoundGrid1.Model))

Adding CellType

The following code example illustrates how to add cell type to the GridDataBoundGrid.

C#

//Set the button Type as ImagePushButton.          this.gridDataBoundGrid1.Binder.InternalColumns["Col1"].StyleInfo.CellType = "ImagePushButton";

VB

'Set the button Type as ImagePushButton.
 Me.gridDataBoundGrid1.Binder.InternalColumns("Col1").StyleInfo.CellType = "ImagePushButton"

Sample

https://www.syncfusion.com/downloads/support/directtrac/general/WF-14573_How_to_add_image_to_Push_button_cell-2068574250.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied