Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Bound column field and template column field

Tags:

c#

asp.net

I would like to know about bound column field and template column field in asp.net gridview mainly the difference. Please help me .

like image 496
hari Avatar asked Aug 03 '14 15:08

hari


2 Answers

Boundfield is a column bound direct to the datasource (column in a DB).

A <asp:TemplateField /> is a customized column which can contain either a DB column as such or you may join together columns for display.

Use boundfield to simply display the db column, use TemplateField to do something more fancy such as concatenate 2 db columns as a single gridview column or add some extra text/description/para to the grid that may not come from the db.

Lets see one basic example of when and how to use TemplateFields.

I want to have 2 columns in my grid that represent 2 columns in the db. FirstName and LastName so the GridView markup will have::

<asp:BoundField DataField="FirstName"  />
<asp:BoundField DataField="LastName"  />

But if you want to concatenate them together you need to use Template Fields:: Here eval("FirstName") is called as the data binding expression.

 <asp:TemplateField HeaderText="FullName" >
      <ItemTemplate>
             eval("FirstName") + " " + eval("LastName")
      </ItemTemplate>
 </asp:TemplateField>

Usually and most of the time , we use a template column when we need more than out of the box functionality for the column.

like image 71
R.C Avatar answered Sep 19 '22 19:09

R.C


BoundColumns you can bind to directly. TemplateColumns can contain more complex controls and you have to bind by using a data binding expression.

like image 20
Kr15 Avatar answered Sep 17 '22 19:09

Kr15