Hi Sir/Madam
I want to create ItemTemplate dynamically based on FromMonthDropDownList and ToMonthDropDownList drop down.
Column varies depends upon dates selected. I can not access the FromMonthDropDownList.SelectedValue; in page_init.
If i create the ItemTemplate elsewhere i can not export to Excel.
Is there any workaround to accomplish this task?
protected void Page_Init(object sender, EventArgs e)
{
string FromYearMonth = FromMonthDropDownList.SelectedValue;
string ToYearMonth = ToMonthDropDownList.SelectedValue;
rgPartSalesByVendor.MasterTableView.Columns.Clear();
GridBoundColumn boundColumnVendor = new GridBoundColumn();
boundColumnVendor.DataField = "Vendor";
boundColumnVendor.UniqueName = "Vendor";
boundColumnVendor.HeaderText = "Vendor";
rgPartSalesByVendor.MasterTableView.Columns.Add(boundColumnVendor);
//Bind sales By vendor Grid
SqlDataSource1.SelectParameters["StartMonth"].DefaultValue = FromYearMonth.Substring(4, 2);
SqlDataSource1.SelectParameters["StartYear"].DefaultValue = FromYearMonth.Substring(0, 4); //ddlStartYear.SelectedValue;
SqlDataSource1.SelectParameters["EndMonth"].DefaultValue = ToYearMonth.Substring(4, 2); // ddlEndMonth.SelectedValue;
SqlDataSource1.SelectParameters["EndYear"].DefaultValue = ToYearMonth.Substring(0, 4); //ddlEndYear.SelectedValue;
rgPartSalesByVendor.DataSource = SqlDataSource1;
DataTable dt = null;
DataView dv = null;
dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
for (int idx = rgPartSalesByVendor.MasterTableView.Columns.Count - 1; idx > 0; idx--)
{
rgPartSalesByVendor.MasterTableView.Columns.RemoveAt(idx);
}
if (dv != null)
{
dt = dv.ToTable();
CreateTemplateColumn(dt);
}
private void CreateTemplateColumn(DataTable dt)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.ColumnName == "Vendor" || dc.ColumnName == "vendorID")
continue;
if (dc.ColumnName != "Vendor" && dc.ColumnName != "VendorID")
{
string ColName = dc.ColumnName;
string[] DPMNameAndID = ColName.Split(';');
string DPMName = DPMNameAndID[0];
string DMPId = DPMNameAndID[1];
GridTemplateColumn templateColumn = new GridTemplateColumn();
templateColumn.ItemTemplate = new MyTemplate(ColName, FromMonthDropDownList.SelectedValue, ToMonthDropDownList.SelectedValue);
templateColumn.SortExpression = ColName;
templateColumn.HeaderText = DPMName;
templateColumn.HeaderStyle.Font.Bold = true;
templateColumn.FooterStyle.Font.Bold = true;
templateColumn.FooterAggregateFormatString = "{0:F2}";
templateColumn.DataField = ColName;
templateColumn.Aggregate = Telerik.Web.UI.GridAggregateFunction.Sum;
this.rgPartSalesByVendor.MasterTableView.Columns.Add(templateColumn);
}
}
}
private void CreateTemplateColumn(DataTable dt)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.ColumnName == "Vendor" || dc.ColumnName == "vendorID")
continue;
if (dc.ColumnName != "Vendor" && dc.ColumnName != "VendorID")
{
string ColName = dc.ColumnName;
string[] DPMNameAndID = ColName.Split(';');
string DPMName = DPMNameAndID[0];
string DMPId = DPMNameAndID[1];
GridTemplateColumn templateColumn = new GridTemplateColumn();
templateColumn.ItemTemplate = new MyTemplate(ColName, FromMonthDropDownList.SelectedValue, ToMonthDropDownList.SelectedValue);
templateColumn.SortExpression = ColName;
templateColumn.HeaderText = DPMName;
templateColumn.HeaderStyle.Font.Bold = true;
templateColumn.FooterStyle.Font.Bold = true;
templateColumn.FooterAggregateFormatString = "{0:F2}";
templateColumn.DataField = ColName;
templateColumn.Aggregate = Telerik.Web.UI.GridAggregateFunction.Sum;
this.rgPartSalesByVendor.MasterTableView.Columns.Add(templateColumn);
}
}
}
private class MyTemplate : ITemplate
{
protected HyperLink SealeByVendorTrans;
private string colname;
private string _fromDate;
private string _toDate;
public MyTemplate(string cName,string fromDate,string toDate)
{
colname = cName;
_fromDate = fromDate;
_toDate = toDate;
}
public void InstantiateIn(System.Web.UI.Control container)
{
SealeByVendorTrans = new HyperLink();
SealeByVendorTrans.ID = colname;
SealeByVendorTrans.DataBinding += new EventHandler(SealeByVendorTrans_DataBinding);
container.Controls.Add(SealeByVendorTrans);
}
void SealeByVendorTrans_DataBinding(object sender, EventArgs e)
{
HyperLink link = (HyperLink)sender;
GridDataItem container = (GridDataItem)link.NamingContainer;
link.Text = ((DataRowView)container.DataItem)[colname].ToString();
link.NavigateUrl = "HinoPartsDashBoardTransactions.aspx?TransType=V&type=Vendor&SalesType=T&from=" + _fromDate + "&to=" + _toDate+"&VendorID="+((DataRowView)container.DataItem)["VendorID"].ToString();
}
}
#endregion