How to center image from command column?

4 posts, 0 answers
  1. Flames
    Flames avatar
    2 posts
    Member since:
    Mar 2011

    Posted 22 Mar 2013 Link to this post

     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
  2. Anton
    Admin
    Anton avatar
    167 posts

    Posted 27 Mar 2013 Link to this post

    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.
  3. LEON
    LEON avatar
    11 posts
    Member since:
    Nov 2018

    Posted 14 Nov 2018 in reply to Anton Link to this post

    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
  4. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3573 posts

    Posted 15 Nov 2018 Link to this post

    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.
Back to Top