Sunday, October 24, 2010

Difference between Template Filed and Bound Field

Use bound field to simply display the db column, use template to do something more fancy such as concatonate 2 db columns as 1 gridview column or add some text to the grid that doesn't come from the db.

The BoundField displays the value of specified DataSource field as text.
By using bound field we can bind the data directly by using header text and datafield without using any controls.
Headertext : We can specify header for the data
Datafield : Its the field which gets the data from dataset.

The TemplateField allows for a mix of HTML markup, Web controls, and data-binding syntax.
We can define our own asp.net controls in template field.

I want to have 2 columns in my grid that represent 2 columns in my db.
First name and surname so I have....
<asp:BoundField DataField="FirstName"/>
<asp:BoundField DataField="SurName" />'

But if you want to string them together you could have....

<asp:TemplateField HeaderText="Name"/>
<Itemtemplate>
eval("FirstName") + " " + eval("surname")
</ItemTemplate>
</asp:TemplateField>

so basically you convert a bound field to a template column when you want more than out of the box functionality for the column.
Template columns also come with a edit template tag which offers you more than the standard editing of that gridview row is desired... e.g when in edit mode put a drop down list in this row for me to select from - possibilities are endless

We can use CommandName property in Tamplate Filed but not in Bound Field.

No comments:

Followers

Link