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

RadGrid grouping column how to display friendly name instead of column Id

6 Answers 639 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Milos
Top achievements
Rank 1
Milos asked on 11 Oct 2013, 01:55 PM
Hello,

I have a very simple table called Lokacije that has two columns. First one is IdLokacije (autonumber) and 2nd one is description of Location with name NazivLokacije.
Second table named Uredjaji, has a column that is foreign key of Lokacija named Lokacije.
Table: Uredjaji
    foreign key: Lokacija
Table: Lokacije
    primary key: IdLokacije (int)
    column desc: NazivLokacije
In my grid (that has datasource through SQLDataSource to Uredjaji) i have GridDropDownColumn, that is bound to Lokacija.
Everything works well, i have in edit mode dropdown list showing me NazivLokacije as DisplayValue it is bound to idLokacije.
When i want to group using Lokacija column, i am getting field IdLokacije showing me numbers, and i want to show friendly name NazivLokacije.
So my question is, how to change default grouping column display value from idLokacije to NazivLokacije?
Below is copy code of my GridDropDownColumn and also picture that should explain better

Thank you in advance
<telerik:GridDropDownColumn ColumnEditorID="GridDropDownColumnEditor1" DataField="Lokacija" DataSourceID="SqlDataSourceLokacija" DataType="System.Int64" HeaderText="Lokacija" ListTextField="NazivLokacije" ListValueField="idLokacije" UniqueName="ColumnLokacija" >                      
    <FilterTemplate>
         <telerik:RadComboBox ID="ColumnLokacija" runat="server" AppendDataBoundItems="true" DataSourceID="SqlDataSourceLokacija" DataTextField="NazivLokacije" DataValueField="idLokacije" Height="200px" OnClientSelectedIndexChanged="TitleIndexChanged" SelectedValue='<%# TryCast(Container, GridItem).OwnerTableView.GetColumn("ColumnLokacija").CurrentFilterValue%>'>
              <Items>
                 <telerik:RadComboBoxItem Text="All" />
              </Items>
         </telerik:RadComboBox>
         <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
          <script type="text/javascript">
                 function TitleIndexChanged(sender, args) {
                    var tableView = $find("<%# TryCast(Container,GridItem).OwnerTableView.ClientID %>");
                    tableView.filter("ColumnLokacija", args.get_item().get_value(), "EqualTo"); }
          </script>
         </telerik:RadScriptBlock>
     </FilterTemplate>
</telerik:GridDropDownColumn>

 

6 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 16 Oct 2013, 08:17 AM
Hi Milos,

Unfortunately the RadGrid does not support this functionality build-in. However you can achieve it with some manual code:
protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
     
        RadGrid1.PreRender += RadGrid1_PreRender;
    }
 
    void RadGrid1_PreRender(object sender, EventArgs e)
    {
        GridItem[] groupHeaders = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader);
        foreach (var header in groupHeaders)
        {
            GridGroupHeaderItem headerItem = header as GridGroupHeaderItem;
            string text = (headerItem.GetChildItems()[0] as GridDataItem)["ColumnLokacija"].Text;
            headerItem.DataCell.Text = string.Format("Lokacija: {0}", text);
        }
    }

Please give it try and let me know if it helps you. Looking forward for your reply.

Regards,
Radoslav
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Milos
Top achievements
Rank 1
answered on 24 Oct 2013, 07:47 AM
Thank you very much

Working as I wanted to
0
Milos
Top achievements
Rank 1
answered on 24 Oct 2013, 08:24 AM
There seems to be another issue when using this approach.
If I want to group on some other column, group header item is always displaying text intended for grouping with column Lokacija.
Also grouping on more then one column is causing crash
How can I check for grouping column name, and execute changes only if grouping column is Lokacija

I am trying to make this work like this.
First I make sure, group column is Lokacija. I don't know better approach at the moment then checking inside DataCell.Text
Second, If I have multiple column grouping, I need to be sure that calling headeritem.getChildItems()(0) wont return me next grouping header.
       
Private Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
        Dim groupHeaders As GridItem() = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)
        For Each header As GridGroupHeaderItem In groupHeaders
            Dim headerItem As GridGroupHeaderItem = TryCast(header, GridGroupHeaderItem)
            If headerItem.DataCell.Text.ToLower.Contains("lokacija") Then               
                Dim temp = headerItem.GetChildItems()(0)
                While TypeOf temp Is GridGroupHeaderItem
                    temp = TryCast(headerItem.GetChildItems()(0), GridGroupHeaderItem).GetChildItems()(0)
                End While
                Dim griditem As GridDataItem = TryCast(temp, GridDataItem)
                If griditem IsNot Nothing Then
                    Dim text As String = griditem("ColumnLokacija").Text
                    headerItem.DataCell.Text = String.Format("Lokacija: {0}", text)
                End If
            End If
        Next
    End Sub

Please let me know if this is valid

Thanks
Milos
0
Radoslav
Telerik team
answered on 28 Oct 2013, 07:54 AM
Hello Milos,

Indeed when you have more than one group expressions into the RadGrid the GridGroupHeaderItem.GetChildItems() will return the inner group. So in this case you need to iterate to the most inner item which is the GridDataItem.
Additionally in order to find if the current group is the group which group key is Lokacija, you can try checking the headerItem.GroupIndex value. If the GroupIndex does not contain underscore ( “_” ) into its value then the headerItem belongs to the first group by expression. If it contains one underscore the headerItem belongs to the second group and etc.
So you can iterate over all RadGrid1.MasterTableView.GroupByExpressions items and find the index of the Lokacija group by expression. Then into the RadGrid1_PreRender just check if the headerItem.GroupIndex contains (Lokacija group index – 1) underscores.

Please give it try and let me know if it helps you. Looking forward for your reply.

Regards,
Radoslav
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Milos
Top achievements
Rank 1
answered on 28 Oct 2013, 09:01 AM
Hello,
thank you for your answer.

I played with this scenario during week. I was able to successfully implement grouping header names, but then new issue opened.
We called headerItem.GetChildItems()(0) to get the header name from first child row, so when u try to edit first child row of our group header, group header name disappears. In debug it returns "". So I changed it not to take name from first row, but from first row that is not in edit state. But, this code now is just to much in order for group header to display friendly name.
So I took different approach, with Idea I got from
http://demos.telerik.com/aspnet-ajax/dropdownlist/examples/applicationscenarios/dropdownlistingird/defaultcs.aspx

Basically I used JOINS in my SQLDataSource to get all friendly names I needed, then I changed my radgridcolumns to template, and from there I was simply able to say that when user wants to sort or group column Lokacija, do that using other column NaziLokacija that I pulled with JOIN.
It is much easier this way. The only drawback, if u can say it is drawback, is implementing JOIN and pulling additional columns. So far I don't see any problem making this.
But I would appreciate if you can give me your opinion of this approach, and if needed I am more then willing to paste any part of my code for better understanding.
Thanks in advance
Milos
0
Radoslav
Telerik team
answered on 31 Oct 2013, 08:31 AM
Hi Milos,

Joining the tables and using the described approach is the other way to achieve the desired functionality. Also the performance is pretty the same as in the other case, so you can use this approach without worries.

I will be glad to assist you further.

Regards,
Radoslav
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Milos
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Milos
Top achievements
Rank 1
Share this question
or