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

Combine Datafileds

3 Answers 69 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wack
Top achievements
Rank 1
Wack asked on 01 Oct 2008, 02:58 PM
I am working with grid and i would like to display a Name column as:

Smith, Joe

and sort by last name as well.

I have tried a GridTemplateColumn, which gives me the proper display, but I can't sort.

I have tried GridBoundColumn, which gives me the sort functionality, but i have to display the data in two columns.

How can i use GridBoundColumn to display Datafield="Contact.Lastname, Contact.Firstname"?

3 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 02 Oct 2008, 10:30 AM
Hello Wack,

You can have the following RadGrid template column definition to attain the sorting functionality you are after:

<telerik:GridTemplateColumn DataField="LastName"   
 SortExpression="LastName"   
 HeaderText="Names">  
  <ItemTemplate> 
   <asp:Label runat="server" ID="Label1" 
     Text='<%#Eval("LastName") + ", " + Eval("FirstName")%>'>  
   </asp:Label> 
  </ItemTemplate> 
</telerik:GridTemplateColumn> 

More information about how to implement sorting for template columns you can find in these resources:

http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/GeneralFeatures/ColumnTypes/DefaultCS.aspx (see the last column in the grid)
http://www.telerik.com/help/aspnet-ajax/grdsortingforhyperlinktemplatecolumns.html

Best regards,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Wack
Top achievements
Rank 1
answered on 02 Oct 2008, 12:02 PM
The example that you provied is basically what i have now.  If i were referencing User.Lastname, then it wouldn't be an issue because my sort expression could be Lastname.  However, they way that the db is designed, I need to reference User.Contact.Lastname, and Contact.Lastname is NOT a valid sort expression within a GridTemplateColumn.  This is why I am at a loss. So what i am saying is, this does not sort.  It displays the data fine, but when i try and sort, I get the "Cannot find column Contact.LastName" error:

<telerik:GridTemplateColumn DataField="Contact.LastName"
    SortExpression="Contact.LastName" HeaderText="Name"> 
        <ItemTemplate> 
            <asp:Label runat="server" ID="Label1" 
                Text='<%#Eval("Contact.LastName") + ", " + Eval("Contact.FirstName")%>'> 
            </
asp:Label> 
        </ItemTemplate>
</telerik:GridTemplateColumn>

Again, my other columns sort fine, and if i seperate the two columns and use a GridBoundColumn like this:

<
telerik:GridBoundColumn DataField="Contact.LastName" HeaderText="Last Name" ItemStyle-Width="80px" />
<telerik:GridBoundColumn DataField="Contact.Firstname" HeaderText="First Name" ItemStyle-Width="80px" />

I can sort just fine, but my data obviously displays as two columns, Last and First name.

Oddly enough, this workaround works.  If I use both the GridBoundColumn and set visable to false, and then display my template column as shown below, i get the functionality I need...but there must be a better way.

<telerik:GridBoundColumn DataField="Contact.LastName" HeaderText="Last Name" ItemStyle-Width="80px" Visible="false" />
<telerik:GridTemplateColumn DataField="Contact.LastName"
    SortExpression="Contact.LastName" HeaderText="Name"> 
        <ItemTemplate> 
            <asp:Label runat="server" ID="Label1" 
                Text='<%#Eval("Contact.LastName") + ", " + Eval("Contact.FirstName")%>'> 

            </
asp:Label> 
        </ItemTemplate>
</telerik:GridTemplateColumn>

0
Nikolay Rusev
Telerik team
answered on 06 Oct 2008, 01:03 PM
Hello Wack,

Unfortunately the GridTemplateColumn won't be able to evaluate nested properties as a sort expression.
The workaround you use - GridBoundColumn with Visible set to false is one possible workaround for this.

All the best,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Wack
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Wack
Top achievements
Rank 1
Share this question
or