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

Controls in the Grid View Header

3 Answers 178 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sasmita
Top achievements
Rank 1
Sasmita asked on 24 Jul 2008, 08:24 PM
Hi ,

I have a Grid, I want to put a check box in the Grid Header.

I mean let's say Header Displays ID, Name and address.
Then fourth column should be something like one Checkbox followed by HeaderText Location . If not check box then any kind button control will also do.

Is there a way i can do it.
 
Thanks,
Sasmita

3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 25 Jul 2008, 09:48 AM
Hello Sasmita,

Thank you for contacting us.

Yes, this can be done by using custom cells. You should process the CreateRow event and create custom header row containing custom cell elements. Consider the following code snippet:

void radGridView1_CreateRow(object sender, GridViewCreateRowEventArgs e) 
    if (e.RowInfo is GridViewTableHeaderRowInfo) 
    { 
        e.RowType = typeof(MyHeaderRow); 
    } 
public class MyHeaderRow : GridTableHeaderRowElement 
    protected override GridCellElement CreateCell(GridViewColumn column) 
    { 
        GridViewDataColumn dataColumn = column as GridViewDataColumn; 
        if (dataColumn != null && dataColumn.FieldName == "Name"
        { 
            return new MyHeaderCell(column, this); 
        } 
        return base.CreateCell(column); 
    } 
 
public class MyHeaderCell : GridHeaderCellElement 
    private RadCheckBoxElement checkbox; 
 
    public MyHeaderCell(GridViewColumn column, GridRowElement row) 
        : base(column, row) 
    { 
    } 
 
    protected override void CreateChildElements() 
    { 
        base.CreateChildElements(); 
 
        this.checkbox = new RadCheckBoxElement(); 
        this.checkbox.ToggleStateChanged += new StateChangedEventHandler(checkbox_ToggleStateChanged); 
        this.Children.Add(checkbox); 
        ApplyThemeToElement(checkbox, "ControlDefault");  
    } 
 
    void checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args) 
    { 
        MessageBox.Show("Checkbox state changed!"); 
    } 
 
    protected override SizeF ArrangeOverride(SizeF finalSize) 
    { 
        SizeF size = base.ArrangeOverride(finalSize); 
        this.checkbox.Arrange(new RectangleF(new PointF(4f,  
            (finalSize.Height - this.checkbox.DesiredSize.Height) / 2f), this.checkbox.DesiredSize)); 
        return size; 
    } 
 
    private void ApplyThemeToElement(RadElement item, string themeName) 
    { 
        DefaultStyleBuilder builder = ThemeResolutionService.GetStyleSheetBuilder(item.ElementTree.ComponentTreeHandler,  
            item.GetThemeEffectiveType().FullName, string.Empty, themeName) as DefaultStyleBuilder; 
        if (builder != null
        { 
            item.Style = new XmlStyleSheet(builder.Style).GetStyleSheet();  
        } 
    }  

As a last step you should eventually modify the theme. This is necessary if the cell style is registered for element type instead of class name. This is true for the default Vista theme. I have attached a modified version of that theme.

I hope this helps. Do not hesitate to write me if you have other questions.

All the best,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Garima Mehta
Top achievements
Rank 1
answered on 05 Jan 2010, 07:33 AM

Hi, I followed all steps mentioned in above post to add check box in header row of grid. Everything is fine except theme. Function ApplyThemeToElementis not working, I am getting error because item.ElementTree is null so i am not able to fetch ComponentTreeHandler. Can you please help?

0
Martin Vasilev
Telerik team
answered on 07 Jan 2010, 02:09 PM
Hi Garima Mehta,

In the latest version Q3 2009 SP1 we changed life cycle of the RadElements and that is why ElementTree still is not initialized when the ApplyThemeToElement has been called. To resolve this, you have to override Initialize method and call ApplyThemeToElement there, but not in CreateChildElements method:

public override void Initialize() 
    base.Initialize(); 
    ApplyThemeToElement(_checkbox, "ControlDefault"); 
}

I hope this helps. Do not hesitate to contact us again if you have any additional questions.

Sincerely yours,
Martin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Sasmita
Top achievements
Rank 1
Answers by
Jack
Telerik team
Garima Mehta
Top achievements
Rank 1
Martin Vasilev
Telerik team
Share this question
or