brainimus
brainimus

Reputation: 11046

GridBoundColumn with multiple DataFields

I have a GridBoundColumn that I would like to be bound to 2 fields so that I can display the two fields in one column. I would like to do something like the following:

<GridBoundColumn DataField1="LastName" DataField2="FirstName" DataFormatString="{0},{1}">

Is this possible? If so how can it be accomplished?

This is used in a Telerik RadGrid if that makes any difference.

Upvotes: 2

Views: 5441

Answers (2)

Atanas Korchev
Atanas Korchev

Reputation: 30671

You can also use a template column if you don't want to write C# code.

Upvotes: 1

brainimus
brainimus

Reputation: 11046

This can be accomplished by implementing the OnItemDataBound method (configured in your grid definition like OnItemdataBound="GridItemDataBound").

Make sure that the field is uniquely identified:

<GridBoundColumn UniqueName="UserName">

Then implement your OnItemDataBound method:

protected void GridItemDataBound(object aSender, GridItemEventArgs anEventArgs)
{
   if(anEventArgs is GridDataItem)
   {
      string firstName = "Joe";
      string lastName = "Smith";
      GridDataItem item = (GridDataItem)anEventArgs.Item;
      item["UserName"].Text = lastName + "," + firstName;
   }
}

Upvotes: 5

Related Questions