I would like to know about bound column field and template column field in asp.net gridview mainly the difference. Please help me .
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.
BoundColumns you can bind to directly. TemplateColumns can contain more complex controls and you have to bind by using a data binding expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With