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

Change layout of the columns in columnchooser

2 Answers 121 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Wesley
Top achievements
Rank 1
Wesley asked on 11 Oct 2012, 01:03 PM
Hi,
The text in the columnbuttons of the columnchooser of a radgridview is aligned in the middle (see sreenshot).
My customer wants the text aligned on the left.
How can I achieve this?

2 Answers, 1 is accepted

Sort by
0
Accepted
Svett
Telerik team
answered on 15 Oct 2012, 12:50 PM
Hi Wesley,

You should create a custom cell element to alter the default behavior of the column chooser, when is shown by the context menu. You can use the following code snippet:
public class CustomGridHeaderCellElement : GridHeaderCellElement
{
    private static readonly MethodInfo MenuItemColumnChooserClickMethodInfo;
 
    static CustomGridHeaderCellElement()
        {
            MenuItemColumnChooserClickMethodInfo = typeof(GridHeaderCellElement).GetMethod("MenuItemColumnChooser_Click",
                BindingFlags.Instance | BindingFlags.NonPublic);
        }
 
    private EventHandler MenuItemColumnChooserClickEventHandler;
 
    public CustomGridHeaderCellElement(GridViewColumn column, GridRowElement row)
        : base(column, row)
        {
            MenuItemColumnChooserClickEventHandler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, MenuItemColumnChooserClickMethodInfo);
        }
 
    protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(GridHeaderCellElement);
            }
        }
 
    protected override void CreateColumnChooserMenuItems(RadDropDownMenu contextMenu)
        {
            base.CreateColumnChooserMenuItems(contextMenu);
 
            RadMenuItem menu = this.GetMenuItem(contextMenu, RadGridLocalizationProvider.CurrentProvider.GetLocalizedString(RadGridStringId.ColumnChooserMenuItem));
 
            if (menu != null)
            {
                menu.Click -= MenuItemColumnChooserClickEventHandler;
                menu.Click += new EventHandler(menu_Click);
            }
        }
 
    private RadMenuItem GetMenuItem(RadDropDownMenu contextMenu, string text)
        {
            foreach (RadItem item in contextMenu.Items)
            {
                RadMenuItem menuItem = item as RadMenuItem;
 
                if (menuItem != null && menuItem.Text == text)
                {
                    return menuItem;
                }
            }
 
            return null;
        }
 
    private void menu_Click(object sender, EventArgs e)
        {
            StackLayoutElement elementHolder = this.GridViewElement.ColumnChooser.ColumnChooserControl.ColumnChooserElement.ElementsHolder;
            elementHolder.ChildrenChanged += this.OnChildrenChanged;
            MenuItemColumnChooserClickEventHandler(sender, e);
        }
 
    private void OnChildrenChanged(object sender, Telerik.WinControls.ChildrenChangedEventArgs e)
        {
            if (e.ChangeOperation == ItemsChangeOperation.Inserted)
            {
                LightVisualElement item = e.Child as LightVisualElement;
                item.TextAlignment = ContentAlignment.MiddleLeft;
            }
        }
}

Then you should use the CreateCell event to replace the default one:
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridHeaderCellElement))
    {
        e.CellType = typeof(CustomGridHeaderCellElement);
    }
}

I hope this helps.

Greetings,
Svett
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
0
Wesley
Top achievements
Rank 1
answered on 08 Nov 2012, 09:12 AM
Works like a sharm.
Thanks a lot.
Tags
GridView
Asked by
Wesley
Top achievements
Rank 1
Answers by
Svett
Telerik team
Wesley
Top achievements
Rank 1
Share this question
or