In RadGrid, how can we merge 2 datacolumns only for viewing, but still be able to edit them separately?

noops picture noops · Jul 18, 2010 · Viewed 7.4k times · Source

I have a Telerik's RadGrid which has 2 columns like this:

<Columns>
 <telerik:GridBoundColumn HeaderText="AirlineCode" UniqueName="AirlineCode" DataField="airlineCode" />
 <telerik:GridBoundColumn HeaderText="FlightNumber " EditFormColumnIndex="1" DataField="flightNumber" />
 ... 
 ... 
 ... more code, but unrelevant to the question here.
</Columns>

I am supplying the data for both columns in the relevant NeedDataSource() function.

So it renders correctly like this:

 | AirlineCode      |      FlightNumber   | 
 ------------------------------------------
 | Delta            |      2393           | 
 | Southwest        |      345            | 

But now my requirement has changed a little bit.

For viewing, I want to merge them together and show it like this:

 |     Flight             |
 --------------------------
 |     Delta-2393         | 
 |     Southwest-345      | 

However, while editing the rows the user should be able to edit "AirlineCode" and "Flight Number" separately. And the values should still be correctly maintained in the datasource.

I know that if we want the user to "View" and "Edit" differently, we would have to use .

So I am trying something like this:

<Columns>
 <telerik:GridTemplateColumn EditFormColumnIndex="0" HeaderText="Flight">
   <ItemTemplate>
  <%#DataBinder.Eval(Container.DataItem, "airlineCode")%>
  <asp:Literal runat="server" Text="-"></asp:Literal>
  <%#DataBinder.Eval(Container.DataItem, "flightNumber")%>
   </ItemTemplate>
   <EditItemTemplate>
  <telerik:GridBoundColumn HeaderText="AirlineCode" UniqueName="AirlineCode" DataField="airlineCode" />
  <telerik:GridBoundColumn HeaderText="FlightNumber " EditFormColumnIndex="1" DataField="flightNumber" />
   </EditItemTemplate>
 </telerik:GridTemplateColumn> ... 
 ... 
 ... more code, but unrelevant to the question here.
</Columns>

But its not working.

Those 2 lines inside are giving warnings:

Element 'GridBoundColumn' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.

Probably I am doing it wrong. Need help.

Any help is appreciated.

Answer

Todd picture Todd · Jul 20, 2010

drpcken is correct. When you use the GridTemplateColumn, you do not need to use the GridBoundColumn. Instead, you supply the View and Edit template HTML and use the Bind expression to do two-way binding in the Edit template. For example:

<telerik:GridTemplateColumn UniqueName="TemplateColumn">                          
         <ItemTemplate>
              <%# Eval("airlinCode") %> - <%# Eval("flightNumber") %>
          </ItemTemplate>
          <EditItemTemplate>
              <table>
                 <tr>
                   <td style="width: 50%">
                     <asp:TextBox runat="server" Text='<%# Bind("airlineCode") %>' />
                   </td>
                   <td style="width: 50%">
                      <asp:TextBox runat="server" Text='<%# Bind("flightNumber") %>' />
                   </td>
                  </tr>
               </table>
          </EditItemTemplate>
</telerik:GridTemplateColumn>

As you can see, you use Eval in the ItemTemplate and Bind in the EditItemTemplate. All other code should continue to work without change.

Let me also highlight the Telerik Forums. For Telerik specific questions, there is an active community available to help troubleshoot: www.telerik.com/forums