Can anybody please let me know difference between difference between templatefield and boundfield and which one is better when adding and removing columns dynamically from gridview.
I used TemplateField column initialy which was working well with static columns but as per my requirement user select element from dropdown list and the columns on gridview change as per that selection now i am facing adding columns dynamically
with TemplateField
When you bind GridView with some columns (ID,Name,City etc...) these columns will display as BoundFields. Bound means which cols you bound to gridview or any control.
And about templatefield: Suppose, you want to make some change in existing display (means instead of Label control if you want to display any data in TextBox / Button then you can use this field).
I used templatefield for same purpose i update my database from gridview so templatefiled helping me out but when my columns get changed then the problem starts
Initialy i display data from AnnualTable which is having columns Task_Id,AnnualYear,FromDate,ToDate,Task,Subtask,Responsibility,Timeline,Bsc,Weightage and then monthly suppose to have all these columns plus some extra columns such as FirstWeek,SecondWeek
so on so what i did is i have done normalisation and kept only Task_Id,FirstWeek,SecondWeek,ThirdWeek,FourthWeek columns inside my month table so whenever user want to see data from month table i do join and display data but gridview which was showing Annual
data initialy do not show any change even after i bind it Month table.
Can anybody please let me know difference between difference between templatefield and boundfield and which one is better when adding and removing columns dynamically from gridview.
TemplateField is more flexible than boundfield. Both of them will add one column to your gridview, however templateField will allw you to customize this column. In your example, you can add validation controls in your edit Template to validate TextBox control.
You can add even complex tables into your column. You dont have this flexibility in boundColumn. I dont think there is big difference in terms of adding dynamic columns. I am not sure about it.
please mark as answer if it helps you
Marked as answer by vinayak bamane on May 21, 2012 05:33 AM
vinayak bama...
Member
39 Points
37 Posts
Difference between templatefield and boundfield
May 18, 2012 05:09 AM|LINK
Hi All,
Can anybody please let me know difference between difference between templatefield and boundfield and which one is better when adding and removing columns dynamically from gridview.
I used TemplateField column initialy which was working well with static columns but as per my requirement user select element from dropdown list and the columns on gridview change as per that selection now i am facing adding columns dynamically with TemplateField
<asp:TemplateField HeaderText="task_Id">
<ItemTemplate>
<asp:Label ID="editIDLabel" runat="server" Text='<%# Bind("Task_Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="editIDTextbox" Columns="2" runat="server" Text='<%# Bind("Task_Id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Here is my code for template field so can anybody please let me know how same can be done by making use of BoundField
Silverlight....
Member
431 Points
369 Posts
Re: Difference between templatefield and boundfield
May 18, 2012 05:52 AM|LINK
Hi Vinayak,
When you bind GridView with some columns (ID,Name,City etc...) these columns will display as BoundFields. Bound means which cols you bound to gridview or any control.
And about templatefield: Suppose, you want to make some change in existing display (means instead of Label control if you want to display any data in TextBox / Button then you can use this field).
Hope this will help you....
vinayak bama...
Member
39 Points
37 Posts
Re: Difference between templatefield and boundfield
May 18, 2012 06:01 AM|LINK
Thanks for your reply
I used templatefield for same purpose i update my database from gridview so templatefiled helping me out but when my columns get changed then the problem starts
Initialy i display data from AnnualTable which is having columns Task_Id,AnnualYear,FromDate,ToDate,Task,Subtask,Responsibility,Timeline,Bsc,Weightage and then monthly suppose to have all these columns plus some extra columns such as FirstWeek,SecondWeek so on so what i did is i have done normalisation and kept only Task_Id,FirstWeek,SecondWeek,ThirdWeek,FourthWeek columns inside my month table so whenever user want to see data from month table i do join and display data but gridview which was showing Annual data initialy do not show any change even after i bind it Month table.
I am frustated with this not getting what to do.
mezzanine74
Contributor
2480 Points
730 Posts
Re: Difference between templatefield and boundfield
May 18, 2012 08:26 AM|LINK
TemplateField is more flexible than boundfield. Both of them will add one column to your gridview, however templateField will allw you to customize this column. In your example, you can add validation controls in your edit Template to validate TextBox control. You can add even complex tables into your column. You dont have this flexibility in boundColumn. I dont think there is big difference in terms of adding dynamic columns. I am not sure about it.
vinayak bama...
Member
39 Points
37 Posts
Re: Difference between templatefield and boundfield
May 18, 2012 09:19 AM|LINK
hi thanks for your reply,
now i am trying it by writing programatically now the data appers on gridview but on click on Edit button again data get disapperas.
public class GridViewTemplate:ITemplate
{
ListItemType tType;
string colName;
public GridViewTemplate(ListItemType type,string cName)
{
tType = type;
colName = cName;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch(tType)
{
case ListItemType.Header:
Label lbl=new Label();
lbl.Text = colName;
container.Controls.Add(lbl);
break;
case ListItemType.Item:
Label txt = new Label();
txt.DataBinding += new EventHandler(txt_DataBinding);
container.Controls.Add(txt);
break;
case ListItemType.EditItem:
if(colName=="Task_Id")
{
TextBox editIDTextbox = new TextBox();
editIDTextbox.DataBinding += new EventHandler(edit_DataBinding);
container.Controls.Add(editIDTextbox);
}
break;
}
}
public void txt_DataBinding(object sender,EventArgs e)
{
Label txtData = (Label)sender;
GridViewRow container = (GridViewRow)txtData.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem,colName);
if(dataValue!=DBNull.Value)
{
txtData.Text = dataValue.ToString();
}
}
public void edit_DataBinding(object sender,EventArgs e)
{
TextBox txtData = (TextBox)sender;
GridViewRow container = (GridViewRow)txtData.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, colName);
if (dataValue != DBNull.Value)
{
txtData.Text = dataValue.ToString();
}
}
}
from here i am calling the above class methods
TemplateField tField = new TemplateField();
tField.HeaderTemplate = new GridViewTemplate(ListItemType.Header,col.ColumnName);
tField.ItemTemplate = new GridViewTemplate(ListItemType.Item,col.ColumnName);
tField.EditItemTemplate = new GridViewTemplate(ListItemType.EditItem, col.ColumnName);
mgm_GridView.Columns.Add(tField);
please help.
ravi_y4u
Member
201 Points
76 Posts
Re: Difference between templatefield and boundfield
May 18, 2012 11:14 AM|LINK
If it is only for displaying data we should be using boundfield.
If the requirement is also include editing the data we should use Template field.
Y.Ravi
vinayak bama...
Member
39 Points
37 Posts
Re: Difference between templatefield and boundfield
May 21, 2012 05:32 AM|LINK
Hi i solved problem by making use of templatefield thanks everybody for your replies