I created GridView and created "BoundColumn" at runtime. When I try to
create TemplateColumn it throws some error. But the same is working in
Design Time Setting.
Design code (Working fine)
---------------------------------------
<asp:TemplateField HeaderText="New col">
<ItemTemplate>
<!--<asp:Literal ID="LControl1" runat="server"
Text='<%# (DataBinder.Eval(Container.DataItem,
"STR_TRAN_DT")).ToString().Trim().Substring(1, 10) %>'></asp:Literal>--
><asp:Literal ID="Literal1" runat="server"
Text='<%# Convert.ToDateTime(DataBinder.Eval(Container.DataI tem,
"STR_TRAN_DT")).ToString("dd/MM/yyyy") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
Code in Codebehing file (Throws error)
-------------------------------------------------------
TemplateField TF_Date = new TemplateField();
string strLtrVal = "<%#
Convert.ToDateTime(DataBinder.Eval(Container.DataI tem, \"STR_TRAN_DT
\")).ToString(\"dd/MM/yyyy\") %>";
TF_Date.ItemTemplate = new
CreateItemTemplateLtrl("STR_TRAN_DT", strLtrVal, "STR_TRAN_DT");
GridView1.Columns.Add(TF_Date);
CreateTemplateLtrl class file
-----------------------------------------
public class CreateItemTemplateLtrl : ITemplate
{
string strddlName;
string strDataValueField;
string strDataTextField;
//Here, I added the constructor for creating the Literal with
value member and text member.
public CreateItemTemplateLtrl(string LtrlName, string
DataValueField, string DataTextField)
{
this.strDataValueField = DataValueField;
this.strDataTextField = DataTextField;
this.strddlName = LtrlName;
}
//Here is the code for declaring DataBinding event for the Ltrl
being added. This occurs while DataGrid is being bound.
public void InstantiateIn(Control objContainer)
{
Literal _LtrlCon = new Literal();
_LtrlCon.DataBinding += new EventHandler(Ltrl_DataBinding);
objContainer.Controls.Add(_LtrlCon);
}
//Here, you can assign all the properties and every thing else
required for the DDL.
private void Ltrl_DataBinding(object sender, EventArgs e)
{
Literal _LtrlCon = (Literal)sender;
_LtrlCon.ID = strddlName;
//_LtrlCon.Text =
(string)DataBinder.Eval("Container.DataItem", strDataValueField);
_LtrlCon.Text = strDataValueField;
//_LtrlCon.DataTextField = strDataTextField;
}
}
--------------------------------
Can anyone please help me!.