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

button in first header cell

6 Answers 138 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hans
Top achievements
Rank 1
Hans asked on 03 Sep 2014, 08:36 AM
How can I place a button in the first header cell above the row selection arrows?  I would like to do this in a class derived from gridview.

Hans Heuvelman

6 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Sep 2014, 07:13 AM
Hello Hans,

Thank you for writing.

You can place a RadButtonElement in the current row's header cell above the row selection arrow by creating a custom GridRowHeaderCellElement. Our GridView >> Creating custom cells help article is quite useful on this topic. Here is a sample code snippet demonstrating the approach for achieving your goal:
 
public Form1()
{
    InitializeComponent();
    this.radGridView1.TableElement.RowHeight = 30;
}
 
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridRowHeaderCellElement))
    {
        e.CellElement = new CustomGridRowHeaderCellElement(e.Column, e.Row);
    }
}
 
public class CustomGridRowHeaderCellElement : GridRowHeaderCellElement
{
    RadButtonElement btn;
    LightVisualElement imageElement;
 
    public RadButtonElement Button
    {
        get
        {
            return btn;
        }
    }
 
    public LightVisualElement ImageElement
    {
        get
        {
            return this.imageElement;
        }
    }
 
    public CustomGridRowHeaderCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
    {
    }
 
    protected override Type ThemeEffectiveType    
    {
        get   
        {
            return typeof(GridRowHeaderCellElement);    
        }
    }
 
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        this.Children.RemoveAt(0);
 
        DockLayoutPanel panel = new DockLayoutPanel();
 
        btn = new RadButtonElement();
        btn.MinSize = new Size(15, 10);
        DockLayoutPanel.SetDock(btn, Telerik.WinControls.Layouts.Dock.Top);
     
        panel.Children.Add(btn);
         
        imageElement = new LightVisualElement();
        imageElement.Margin = new System.Windows.Forms.Padding(0, 0, 0, 2);
        panel.Children.Add(imageElement);  
        DockLayoutPanel.SetDock(imageElement, Telerik.WinControls.Layouts.Dock.Bottom);
        this.Children.Add(panel);
    }
}
 
private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    CustomGridRowHeaderCellElement cell = e.CellElement as CustomGridRowHeaderCellElement;
    if (cell != null)
    {
        cell.ImageElement.Image = cell.Image;
        cell.Image = null;
        if (cell.RowInfo.IsCurrent)
        {
            cell.Button.Visibility = Telerik.WinControls.ElementVisibility.Visible;
        }
        else
        {
            cell.Button.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        }
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Hans
Top achievements
Rank 1
answered on 08 Sep 2014, 07:50 AM
Unforunately the buttom appears in the first cell of the datarows in stead of the first headercell left of 'column1'.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Sep 2014, 04:29 PM
Hello Hans,

Thank you for writing back.

On my end the button appears correctly in the row header cell above the arrow image indicating the current row. I have attached my sample project.

If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Hans
Top achievements
Rank 1
answered on 09 Sep 2014, 06:28 AM
But I need the button in the first cell of header-row, the cell before header-cell 'ProductID'.  Not in the current row.
See attached picture.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 11 Sep 2014, 03:42 PM
Hello Hans,

Thank you for writing back.

The provided picture helped me to understand completely the exact requirement. Please find below the modified sample code demonstrating how to achieve the result from the attached screenshot:
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridTableHeaderCellElement))
    {
        e.CellElement = new CustomGridRowHeaderCellElement(e.Column, e.Row);
    }
}
 
public class CustomGridRowHeaderCellElement : GridTableHeaderCellElement
{
    RadButtonElement btn;
 
    public CustomGridRowHeaderCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
    {
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridTableHeaderCellElement);
        }
    }
 
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        this.Children.RemoveAt(0);
        btn = new RadButtonElement();
        btn.MinSize = new Size(15, 10);
        this.Children.Add(btn);
    }
}

I hope this information helps. If you have any additional questions, please let me know.
 
Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Hans
Top achievements
Rank 1
answered on 12 Sep 2014, 07:19 AM
This is exactly what I was looking for.  Thanks a lot.
Tags
GridView
Asked by
Hans
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Hans
Top achievements
Rank 1
Share this question
or