[HOWTO] Set GridGroupField HeaderText Programatically

3 posts, 1 answers
  1. Joel
    Joel avatar
    22 posts
    Member since:
    Sep 2012

    Posted 26 Aug 2014 Link to this post

    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. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 26 Aug 2014 in reply to Joel Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Joel
    Joel avatar
    22 posts
    Member since:
    Sep 2012

    Posted 26 Aug 2014 in reply to Jayesh Goyani Link to this post

    Thanks, Jayesh, those are great options!

    Sorry my code block formatting didn't work. :)
Back to Top