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

[HOWTO] Set GridGroupField HeaderText Programatically

2 Answers 54 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Joel asked on 26 Aug 2014, 04:42 PM
I've got a default GroupByExpression selecting a column, and it works great.

I already set the column titles programatically because they are translated in different languages. I was trying to figure out how do I set the HeaderText for the GridGroupField programatically? It doesn't automatically get the column title, I can't databind via ASP.NET (like <%# GetTitle() %>), and I don't see a created/databound item to alter in the same way I'm altering many items.

ASPX:
[code]
 <GroupByExpressions>
  <telerik:GridGroupByExpression>
    <SelectFields>
      <telerik:GridGroupByField FieldName="t_rv_account_group" HeaderText="CUSTOM VALUE HERE"/>
    </SelectFields>
    <GroupByFields>
      <telerik:GridGroupByField FieldName="t_rv_account_group" />
    </GroupByFields>
  </telerik:GridGroupByExpression>
</GroupByExpressions>[/code]

It seems obvious now I've figured it out, but it took me enough time I thought I'd share. Since these are part of the HTML/ASP object, they of course are part of the object model, and can be modified in RadGrid1_PreRender() and can be set at the same time I'm setting translated column display titles.

C# CodeBehind:
[code]RadGrid1.MasterTableView.GroupByExpressions[0].SelectFields[0].HeaderText = CustomValueHereTitle;[/code]
This sets the specific one I knew about, or you can iterate the lists and map titles based on FieldName values.


2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Aug 2014, 07:35 PM
Hello,

Please try with the below code snippet.

void RadGrid1_PreRender(object sender, EventArgs e)
    {
        GridItem[] groupHeaders = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader);
        foreach (var header in groupHeaders)
        {
            GridGroupHeaderItem headerItem = header as GridGroupHeaderItem;
               // You can able to access column text here
            string text = (headerItem.GetChildItems()[0] as GridDataItem)["ColumnName"].Text; 

            headerItem.DataCell.Text = "Your new text comes here";
        }
    }

OR

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   
    if (e.Item is GridGroupHeaderItem)
    {
        GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
        DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
        item.DataCell.Text = "Your new text comes here";
         
    }
}

Thanks,
Jayesh Goyani
0
Joel
Top achievements
Rank 1
answered on 26 Aug 2014, 10:08 PM
Thanks, Jayesh, those are great options!

Sorry my code block formatting didn't work. :)
Tags
Grid
Asked by
Joel
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Joel
Top achievements
Rank 1
Share this question
or