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

Dynamic created GridTemplateColumn formatting

1 Answer 117 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Balkar
Top achievements
Rank 1
Balkar asked on 27 Jun 2013, 08:43 PM
Hi,
  I have a dynamic created grid and the Grid have GridTemplateColum which contains a hyperlink. I want To format Hyperlink Text to currency. I tried below, but does not work
string Price = ((DataRowView)container.DataItem)[colname].ToString();
           link.Text = String.Format("{0:C}", Price);
#region Create GridTemplate column
   private void CreateTemplateColumn(DataTable dt, ref RadGrid rg, string FromYearMonth, string ToYearMonth)
   {
       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, FromYearMonth, ToYearMonth, DPMName, DMPId);
               templateColumn.SortExpression = ColName;
               templateColumn.HeaderText = DPMName;
               templateColumn.HeaderStyle.Font.Bold = true;
               templateColumn.FooterStyle.Font.Bold = true;
               templateColumn.FooterAggregateFormatString = "{0:C}";
               templateColumn.DataField = ColName;
               templateColumn.Aggregate = Telerik.Web.UI.GridAggregateFunction.Sum;
               rg.MasterTableView.Columns.Add(templateColumn);
           }
       }
   }
 
   private class MyTemplate : ITemplate
   {
 
       protected HyperLink SealeByVendorTrans;
 
       private string colname;
       private string _fromDate;
       private string _toDate;
       private string _DPMName;
       private string _DPMId;
       public MyTemplate(string cName, string fromDate, string toDate, string DPMName, string DPMId)
       {
           colname = cName;
           _fromDate = fromDate;
           _toDate = toDate;
           _DPMName = DPMName;
           _DPMId = DPMId;
       }
       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;
           string Price = ((DataRowView)container.DataItem)[colname].ToString();
           //txtPrice.Text = String.Format("{0:C}", price);
           link.Text = String.Format("{0:C}", Price);
              
           link.NavigateUrl = "HinoPartsDashBoardTransactions.aspx?type=Vendor&TransType=V&DPMName=" + _DPMName + "&DPM=" + _DPMId + "&from=" + _fromDate + "&to=" + _toDate + "&VendorID=" + ((DataRowView)container.DataItem)["VendorID"].ToString() + "&Vendor=" + ((DataRowView)container.DataItem)["Vendor"].ToString();
       }
   }
   #endregion

1 Answer, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 02 Jul 2013, 10:31 AM
Hello Balkar,

That presented code does not work for you as the Price is a string value, not a number. The currency format only applies to numbers. Therefore please pass correct int value so that it could be formatted as currency.

Regards,
Maria Ilieva
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Balkar
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Share this question
or