1

I was able to get part of my answer from another question but cannot find a more specific bit of information that I need to finish.

I am trying to combine 2 fields into 1 like the above question but one of them is a GridHyperLinkColumn. I am using a GridItemDataBound event in the code behind to put 2 fields together like this:

protected void GridItemDataBound(object sender, GridItemEventArgs e) {
  if (e.Item is GridDataItem) {
    GridDataItem item = (GridDataItem)e.Item;
    item["A"].Text = item["A"].Text + " /<br/>" + item["B"].Text;
    item["C"].Text = item["C"].Text + " /<br/>" + item["D"].Text;
   }
}

My UI shorten down for simplicity looks like this:

 <Columns>    
    <telerik:GridBoundColumn UniqueName="A" DataField="A" />
    <telerik:GridBoundColumn UniqueName="B" DataField="B" Visible="false" />
    <telerik:GridHyperLinkColumn DataNavigateUrlFields="ID"        DataNavigateUrlFormatString="~.aspx?ID={0}" DataTextField="C" Text="{0}" UniqueName="C" />     
    <telerik:GridBoundColumn UniqueName="D" DataField="D" Visible="false" />
  </Columns>

This works well for the first 2 columns that I combine into 1 (A & B).

However suppose item["C"] is a GridHyperLinkColumn. When I try the same code, it only shows / <D value>. Nothing shows up in front of the slash where "C" should be showing.

Is there a different property (instead of .Text) I should use or do I need to do it a different way?

Community
  • 1
  • 1
Shorty Long
  • 13
  • 1
  • 4

1 Answers1

0

There are a number of ways you could do this. One approach would be to use a template column as show below:

<Columns>
    <telerik:GridTemplateColumn HeaderText="A and B">
        <ItemTemplate><%# Eval("A") %>/<br /><%# Eval("B") %></ItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridTemplateColumn HeaderText="B and C">
        <ItemTemplate><a href="blah.aspx?ID=<%# Eval( "C" ) %>"><%# Eval( "C" ) %></a>/<br /><%# Eval("D") %></ItemTemplate>
    </telerik:GridTemplateColumn>
</Columns>

If you're not interested in using a template column, you can get it to work the way you're doing it. The issue is that the GridHyperlinkColumn renders the link as a control (e.g. item["C"].Controls[0]) instead of directly in the Text property. Here is one way to get it to do what you want:

if (e.Item is GridDataItem) {
    GridDataItem item = (GridDataItem)e.Item;
    item["A"].Text = item["A"].Text + " /<br/>" + item["B"].Text;
    Literal lit = new Literal();
    lit.Text = " /<br/>" + item["D"].Text;
    item["C"].Controls.Add( lit );
}
csm8118
  • 1,213
  • 9
  • 11
  • I tried the GridTemplateColumn method and it did not work - I was getting an "Eval can only be used in the context of a Databound Column" error. I even tried putting it within the GridBoundColumn tags and it complained that ItemTemplate was not valid. However, I was able to get it successfully working using the literal technique (That was pretty slick). Thanks for your help. – Shorty Long Mar 20 '12 at 13:23
  • @ShortyLong Interesting that you couldn't get the first example to work. It worked properly for me when I manually databound a DataTable to the grid. Any rate, glad you could get the second way to work. Please mark my answer as the accepted answer. – csm8118 Mar 20 '12 at 13:50