This is a migrated thread and some comments may be shown as answers.

How to center image from command column?

3 Answers 668 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Flames
Top achievements
Rank 1
Flames asked on 22 Mar 2013, 07:12 PM
 Is there a way to center the 'X' icon on the center of the column 'Remover' ? It's placed on the left and i cannot find a way to move it to the center, image of the grid attached. Thanks anyway, any comments about a better way to do this button column are appreciated too. Thanks

3 Answers, 1 is accepted

Sort by
0
Anton
Telerik team
answered on 27 Mar 2013, 02:20 PM
Hello Flames,

Thank you for writing.

To align the image in the middle of the button, you should subscribe to the CellFormatting event of RadGridView and set the ImageAlignment property to "MiddleCenter". For example:
void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    GridCommandCellElement cmdCell = e.CellElement as GridCommandCellElement;
 
    if (cmdCell != null)
    {
        cmdCell.CommandButton.ImageAlignment = ContentAlignment.MiddleCenter;
 
        if (e.Column.Name == "column3")
        {
            cmdCell.CommandButton.Image = Properties.Resources._1353922628_home;
        }
        if (e.Column.Name == "column4")
        {
            cmdCell.CommandButton.Image = Properties.Resources._1353922617_delete;
        }
    }
}

Attached is a demo project that comprises the code above.

I hope this helps. Do not hesitate to write back if you have further questions.

Kind regards,
Anton
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
0
LEON
Top achievements
Rank 1
answered on 14 Nov 2018, 09:21 PM

this is working fine with C#, but when I convert this to VB its showing error. Trycast block not working for this. is there any workaround for VB.net?

Private Sub radGridView1_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs)
    Dim cmdCell As GridCommandCellElement = TryCast(e.CellElement, GridCommandCellElement)
 
    If cmdCell IsNot Nothing Then
        cmdCell.CommandButton.ImageAlignment = ContentAlignment.MiddleCenter
 
        If e.Column.Name = "column3" Then
            cmdCell.CommandButton.Image = Properties.Resources._1353922628_home
        End If
 
        If e.Column.Name = "column4" Then
            cmdCell.CommandButton.Image = Properties.Resources._1353922617_delete
        End If
    End If
End Sub
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Nov 2018, 12:06 PM
Hello, LEON, 

You can find below the converted VB.NET code snippet how to center the image for the command cell element: 

Private Sub RadGridView1_CellFormatting(sender As Object, e As CellFormattingEventArgs)
    Dim cmdCell As GridCommandCellElement = TryCast(e.CellElement, GridCommandCellElement)
 
    If cmdCell IsNot Nothing Then
        cmdCell.CommandButton.ImageAlignment = ContentAlignment.MiddleCenter
 
        If e.Column.Name = "CommandColumn1" Then
            cmdCell.CommandButton.Image = My.Resources.Camera48
        End If
 
        If e.Column.Name = "CommandColumn2" Then
            cmdCell.CommandButton.Image = My.Resources.calendar
        End If
    End If
End Sub

I hope this information helps.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Flames
Top achievements
Rank 1
Answers by
Anton
Telerik team
LEON
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or