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

User-defined Text for GridColumnGroup and GridBoundColumn

1 Answer 344 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bryan
Top achievements
Rank 1
Bryan asked on 24 Feb 2017, 04:29 AM

I have the following grid (as a sample)

<telerik:RadGrid runat="server" ID="RadGrid1" AllowSorting="true" AllowPaging="true" PageSize="15" AutoGenerateColumns="false" ShowDesignTimeSmartTagMessage="false" Width="9700px" AllowFilteringByColumn="true" GroupingSettings-CaseSensitive="false" >
   <MasterTableView GroupLoadMode="Client" DataKeyNames="Id" NoMasterRecordsText="No records." >
      <ColumnGroups>
         <telerik:GridColumnGroup HeaderText="[Group1]" Name="grp1" />
         <telerik:GridColumnGroup HeaderText="[Group2]" Name="grp2" />
      </ColumnGroups>
      <Columns>
         <telerik:GridBoundColumn UniqueName="Item1a" DataField="Item1a" HeaderText="[Item 1a]"  ColumnGroupName="grp1" AutoPostBackOnFilter="true" FilterListOptions="VaryByDataType" CurrentFilterFunction="Contains" FilterControlWidth="100px">
          <HeaderStyle HorizontalAlign="Left" Width="120px" Wrap="false" />
          <ItemStyle HorizontalAlign="Left" Width="120px" />
         </telerik:GridBoundColumn>
         <telerik:GridBoundColumn UniqueName="Item1b" DataField="Item1b" HeaderText="[Item 1b]"  ColumnGroupName="grp1" AutoPostBackOnFilter="true" FilterListOptions="VaryByDataType" CurrentFilterFunction="Contains" FilterControlWidth="100px">
          <HeaderStyle HorizontalAlign="Left" Width="120px" Wrap="false" />
          <ItemStyle HorizontalAlign="Left" Width="120px" />
         </telerik:GridBoundColumn>
         <telerik:GridBoundColumn UniqueName="Standalone" DataField="Standalone" HeaderText="[Stand Alone]"  AutoPostBackOnFilter="true" FilterListOptions="VaryByDataType" CurrentFilterFunction="Contains" FilterControlWidth="100px">
          <HeaderStyle HorizontalAlign="Left" Width="120px" Wrap="false" />
          <ItemStyle HorizontalAlign="Left" Width="120px" />
         </telerik:GridBoundColumn>
         <telerik:GridBoundColumn UniqueName="Item2a" DataField="Item2a" HeaderText="[Item 2a]"  ColumnGroupName="grp2" AutoPostBackOnFilter="true" FilterListOptions="VaryByDataType" CurrentFilterFunction="Contains" FilterControlWidth="100px">
          <HeaderStyle HorizontalAlign="Left" Width="120px" Wrap="false" />
          <ItemStyle HorizontalAlign="Left" Width="120px" />
         </telerik:GridBoundColumn>
         <telerik:GridBoundColumn UniqueName="Item2b" DataField="Item2b" HeaderText="[Item 2b]"  ColumnGroupName="grp2" AutoPostBackOnFilter="true" FilterListOptions="VaryByDataType" CurrentFilterFunction="Contains" FilterControlWidth="100px">
          <HeaderStyle HorizontalAlign="Left" Width="120px" Wrap="false" />
          <ItemStyle HorizontalAlign="Left" Width="120px" />
         </telerik:GridBoundColumn>
      </Columns>
   </MasterTableView>
</telerik:RadGrid>

 

I am trying to alter the text on both the ColumnGroup items and the GridHeader items.

I can change the ColumnGroup text like this (in ItemDataBound):

RadGrid1.MasterTableView.ColumnGroups.FindGroupByName("grp1").HeaderText = "First Group"

 

But when I try to rename the GridHeader (as I have done numerous times before), there is no change

Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
   If (TypeOf e.Item Is GridHeaderItem) Then
      Dim header As GridHeaderItem = e.Item
      CType(header("Item1a").Controls(0), LinkButton).Text = "Group 1 - Item 1"
      CType(header("Item1b").Controls(0), LinkButton).Text = "Group 1 - Item 2"
      CType(header("Item2a").Controls(0), LinkButton).Text = "Group 2 - Item 1"
      CType(header("Item2b").Controls(0), LinkButton).Text = "Group 2 - Item 2"
   End if

 

What am I doing wrong?

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 27 Feb 2017, 08:51 AM
Hello Bryan,

I've already replied to this query in your formal support thread. I suggest that we continue our conversation on the mentioned thread.

I will also paste the reply here so other developers with similar requirements may use this info:
Generally, you can use the GetHeaderCellByColumnUniqueName() method to achieve this requirement with ColGroups:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-cells-using-column-unique-name

For example, if you want to include an image indicator for the filtered columns:
Copy Code
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns
           .OfType<IGridDataColumn>().Where(x => x.AllowFiltering))
    {
        if (!string.IsNullOrEmpty(col.EvaluateFilterExpression()))
        {
            GridTableHeaderCell cell = RadGrid1.MasterTableView.GetHeaderCellByColumnUniqueName(col.UniqueName);
            cell.BackColor = System.Drawing.Color.Aqua;
            cell.Style["background-image"] = "none";
 
            cell.Controls.Add(new Image()
            {
                ID = "FilterIndicator" + col.UniqueName,
                ImageUrl = "~/arrowUp.png"
            });
        }
    }
}

I hope this will prove helpful.


Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Bryan
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or