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

DataFormatString not working on programatical grid

2 Answers 175 Views
Grid
This is a migrated thread and some comments may be shown as answers.
wonderbison
Top achievements
Rank 1
wonderbison asked on 22 Jun 2011, 10:44 AM
Hi

The DataFormatString doesnt seem to be formatting my data. Working example below. I am building a grid programmatically (although the live example is a hierarchical grid but this example also doesnt work).

Help really appreciated as I looking at the examples on the site I would expect it would

Thanks

aspx code
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="DataFormatting._Default" %>
  
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
  
  
<form id="form1" runat="server">
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
</telerik:RadAjaxManager>
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
</telerik:RadScriptManager>
  
    <div class="contributionTable" >
        <asp:PlaceHolder ID="PlaceHolder1" runat="server" >
        <telerik:RadGrid ID="clientContribution" runat="server" 
            onitemdatabound="clientContribution_ItemDataBound" >
        </telerik:RadGrid>
        </asp:PlaceHolder>
    </div>
  
</form>


Code behind
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
  
namespace DataFormatting
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BuildTheGrid();
            }
  
        }
        protected void clientContribution_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridGroupHeaderItem)
            {
                GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
                DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
  
            }
  
        }
        #region Build the grid
        private DataSet BuildTheGrid()
        {
            try
            {
  
                clientContribution.DataSource = null;
                DataSet contributionColumns = LoadGridData();
  
  
                // Define the main grid
                for (int loopPos = clientContribution.MasterTableView.Columns.Count; loopPos > 0; loopPos--)
                {
                    clientContribution.MasterTableView.Columns.RemoveAt(loopPos - 1);
                }
                clientContribution.ID = "MyGrid";
                clientContribution.DataSource = contributionColumns;
                clientContribution.Width = Unit.Percentage(98);
                clientContribution.AutoGenerateColumns = false;
                clientContribution.ShowStatusBar = true;
                clientContribution.MasterTableView.Width = Unit.Percentage(100);
  
                // now build the hierarchical grid
                GridBoundColumn boundColumn = new GridBoundColumn();
  
                foreach (DataColumn col in contributionColumns.Tables[0].Columns)
                {
                        boundColumn = new GridBoundColumn();
                        clientContribution.MasterTableView.Columns.Add(boundColumn);
                        boundColumn.DataField = col.ColumnName;
                        boundColumn.HeaderText = col.ColumnName;
                        boundColumn.Visible = true;
  
                        if (col.Ordinal == 1)
                        {
                            boundColumn.DataFormatString = "{0:##,###.0}";
                        }
                        if (col.Ordinal == 2)
                        {
                            boundColumn.DataFormatString = "{0:###.0%}";
                        }
  
                }
                clientContribution.DataBind();
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
                return null;
            }
            finally
            {
            }
            return null;
  
        }
        #endregion
  
        #region Load the Grid Data
        private DataSet LoadGridData()
        {
            // return the data to display
            DataSet contributionData = new DataSet("MyData");
  
            DataTable gridData = contributionData.Tables.Add("ContData");
  
            gridData.Columns.Add(new DataColumn("DummyValue"));
            gridData.Columns.Add(new DataColumn("DecimalValue"));
            gridData.Columns.Add(new DataColumn("PercentageValue"));
  
            for (int i = 0; i < 10; i++)
            {
                DataRow dataRow = contributionData.Tables[0].NewRow();
  
                dataRow["DummyValue"] = "Value" + i.ToString();
                dataRow["DecimalValue"] = 1234.88 * i;
                dataRow["PercentageValue"] = 2.33 * i;
  
                contributionData.Tables[0].Rows.Add(dataRow);
            }
            return contributionData;
        }
        #endregion
  
    }
}

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Jun 2011, 12:37 PM
Hello Robin,

Try setting the DataType of the Bound column as Decimal.
C#:
boundColumn = new GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = "Freight";
boundColumn.HeaderText = "Freight";
boundColumn.DataType =typeof(System.Decimal);
boundColumn.DataFormatString = "{0:##.###}";

Thanks,
Princy.
0
wonderbison
Top achievements
Rank 1
answered on 23 Jun 2011, 10:11 AM
Thanks. This helped. What also helped was that the data that is being brought back was from an OLAP SQL Server and the data comes back as a string, even when its numeric. So I was plugging in a string to the column which obviously didnt help.

Thanks
Tags
Grid
Asked by
wonderbison
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
wonderbison
Top achievements
Rank 1
Share this question
or