This is a migrated thread and some comments may be shown as answers.

[Solved] Dynamically Created ItemTemplate for Varying column Based on date selected

2 Answers 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Balkar
Top achievements
Rank 1
Balkar asked on 17 May 2013, 03:41 PM

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

2 Answers, 1 is accepted

Sort by
0
Balkar
Top achievements
Rank 1
answered on 20 May 2013, 04:41 PM
Hi Sir/Madam
 Thanks in advance
  I am able to get drop down value By using the code below in Page_ini

        string FromYearMonth = Request.Form.Get("FromMonthDropDownList");
        string ToYearMonth = Request.Form.Get("ToMonthDropDownList");

Now problem I have my data source SQLdataSource1 varies number of columns depends upon dates selected:
Example 1: Vendor sales1 sales2
Example 2:Vendor sales4 sales5 sales6 sales7 sales8

I am dynamically Creating template Column and assigning Datasource to RadGrid. If the number of column are different from the first selection, I am getting the error. Is there any work around this situation?

Error:Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

 protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeMonthDropDownLists();
        }

        string FromYearMonth = Request.Form.Get("FromMonthDropDownList");
        string ToYearMonth = Request.Form.Get("ToMonthDropDownList");
        if (FromYearMonth == null)
        {
            FromYearMonth = FromMonthDropDownList.SelectedValue;
            ToYearMonth = ToMonthDropDownList.SelectedValue;
        }

        RadGrid rgPartSalesByVendor = new RadGrid();
        rgPartSalesByVendor.AutoGenerateColumns = false;
        rgPartSalesByVendor.Columns.Clear();
        rgPartSalesByVendor.ID = "rgPartSalesByVendor";
        rgPartSalesByVendor.GridLines = System.Web.UI.WebControls.GridLines.Both;
        rgPartSalesByVendor.ShowFooter = true;
        rgPartSalesByVendor.Skin = "Outlook";
        //rgPartSalesByVendor.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(rgPartSalesByVendor_Load);
        rgPartSalesByVendor.MasterTableView.Columns.Clear();
      
        GridBoundColumn boundColumnVendor = new GridBoundColumn();
        boundColumnVendor.DataField = "Vendor";
        boundColumnVendor.UniqueName = "Vendor";
        boundColumnVendor.HeaderText = "Vendor";
        boundColumnVendor.HeaderStyle.Font.Bold = true;
        boundColumnVendor.FooterStyle.Font.Bold = true;
        boundColumnVendor.FooterText = "Grand Total:";
        boundColumnVendor.ItemStyle.ForeColor = System.Drawing.Color.Blue;


        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);

        if (dv != null)
        {
            dt = dv.ToTable();
            CreateTemplateColumn(dt, rgPartSalesByVendor, FromYearMonth, ToYearMonth);
        }
        //rgPartSalesByVendor.Rebind();
        pnlSalesByVendor.Controls.Clear();
        rgPartSalesByVendor.Rebind();
        pnlSalesByVendor.Controls.Add(rgPartSalesByVendor);

        Session.Add("GridPartSalesByVendor", rgPartSalesByVendor);
        

    }
0
Balkar
Top achievements
Rank 1
answered on 20 May 2013, 08:00 PM
Resolved i removed and added the dynamic grid in updatepanel control:
upd1 is updatePanel control and dynamic grid is added inside panel pnlSalesByVendor

Page_load
upd1.ContentTemplateContainer.Controls.RemoveAt(upd1.ContentTemplateContainer.Controls.Count - 1);
        upd1.ContentTemplateContainer.Controls.Add(pnlSalesByVendor);
Tags
Grid
Asked by
Balkar
Top achievements
Rank 1
Answers by
Balkar
Top achievements
Rank 1
Share this question
or