I have a GridNumericColumn with bound field.
Example: 2.45678 will still show 2.45678 but I want 2.46. I tried with DataFormatString {0:0.##} and it does't work too.
What's wrong with this code...
<
telerik:GridNumericColumn
DataField
=
"ppds_surf_corp"
DecimalDigits
=
"2"
FilterControlAltText
=
"Filter colSurfCorp column"
HeaderText
=
"Surface corporelle"
ShowFilterIcon
=
"False"
UniqueName
=
"colSurfCorp"
AllowRounding
=
"True"
DataType
=
"System.Decimal"
>
</
telerik:GridNumericColumn
>
Example: 2.45678 will still show 2.45678 but I want 2.46. I tried with DataFormatString {0:0.##} and it does't work too.
What's wrong with this code...
7 Answers, 1 is accepted
0

Casey
Top achievements
Rank 1
answered on 02 Mar 2012, 08:10 PM
Hi Jocelyn,
I couldn't get it to work in my RadGrid when my DataField is a string. I was able to change the value via code in the ItemDataBound event though. I'm not sure why the DataFormatString property was working.
I hope this helps!
Casey
I couldn't get it to work in my RadGrid when my DataField is a string. I was able to change the value via code in the ItemDataBound event though. I'm not sure why the DataFormatString property was working.
I hope this helps!
Casey
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem dataItm = e.Item
as
GridDataItem;
dataItm[
"colSurfCorp"
].Text = Math.Round(Convert.ToDecimal(dataItm[
"colSurfCorp"
].Text), 2).ToString();
}
}
0

Shinu
Top achievements
Rank 2
answered on 05 Mar 2012, 06:12 AM
Hello,
Here is the sample code that I tried which worked as expected.
aspx:
-Shinu.
Here is the sample code that I tried which worked as expected.
aspx:
<
telerik:GridNumericColumn
UniqueName
=
"UnitPrice"
DataField
=
"UnitPrice"
DataFormatString
=
"{0:#.#0}"
></
telerik:GridNumericColumn
>
-Shinu.
0

Jocelyn
Top achievements
Rank 1
answered on 05 Mar 2012, 02:43 PM
Well, I don't know why but your DataFormating is not working for me. But I tried with a sample project.
This is only a RadGrid showing some columns of the Sales.SalesPerson from AdventureWorks Microsoft DB. I tried your DataFormat on the SalesYTD and it works! But, It doesn't round the decimal. I know this is normal, it's a format string.
What I don't understand is what DecimalDigits stand for if it doesn't affect anything...
Here is my code:
I don't have anything server side.
Thanks.
This is only a RadGrid showing some columns of the Sales.SalesPerson from AdventureWorks Microsoft DB. I tried your DataFormat on the SalesYTD and it works! But, It doesn't round the decimal. I know this is normal, it's a format string.
What I don't understand is what DecimalDigits stand for if it doesn't affect anything...
Here is my code:
<%@ Page Language="VB" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="RadControlsWebApp5._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
></
title
>
<
telerik:RadStyleSheetManager
id
=
"RadStyleSheetManager1"
runat
=
"server"
/>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
runat
=
"server"
>
<
Scripts
>
<%--Needed for JavaScript IntelliSense in VS2010--%>
<%--For VS2008 replace RadScriptManager with ScriptManager--%>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.Core.js"
/>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.jQuery.js"
/>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.jQueryInclude.js"
/>
</
Scripts
>
</
telerik:RadScriptManager
>
<
script
type
=
"text/javascript"
>
</
script
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
</
telerik:RadAjaxManager
>
<
div
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"False"
CellSpacing
=
"0"
DataSourceID
=
"AdventureWorks"
GridLines
=
"None"
AllowFilteringByColumn
=
"True"
>
<
MasterTableView
DataKeyNames
=
"SalesPersonID"
DataSourceID
=
"AdventureWorks"
>
<
CommandItemSettings
ExportToPdfText
=
"Export to PDF"
></
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridEditCommandColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
>
</
telerik:GridEditCommandColumn
>
<
telerik:GridBoundColumn
DataField
=
"SalesPersonID"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter SalesPersonID column"
HeaderText
=
"SalesPersonID"
ReadOnly
=
"True"
SortExpression
=
"SalesPersonID"
UniqueName
=
"SalesPersonID"
Visible
=
"true"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"TerritoryID"
FilterControlAltText
=
"Filter AccountNumber column"
HeaderText
=
"TerritoryID"
SortExpression
=
"TerritoryID"
UniqueName
=
"TerritoryID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"SalesQuota"
FilterControlAltText
=
"Filter Name column"
HeaderText
=
"SalesQuota"
SortExpression
=
"SalesQuota"
UniqueName
=
"SalesQuota"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Bonus"
DataType
=
"System.String"
FilterControlAltText
=
"Filter Bonus column"
HeaderText
=
"Bonus"
SortExpression
=
"Bonus"
UniqueName
=
"Bonus"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"CommissionPct"
FilterControlAltText
=
"Filter CommissionPct column"
HeaderText
=
"CommissionPct"
UniqueName
=
"CommissionPct"
>
</
telerik:GridBoundColumn
>
<
telerik:GridNumericColumn
AllowRounding
=
"True"
DataField
=
"SalesYTD"
DecimalDigits
=
"2"
FilterControlAltText
=
"Filter SalesYTD column"
HeaderText
=
"SalesYTD"
UniqueName
=
"SalesYTD"
DataType
=
"System.Decimal"
>
</
telerik:GridNumericColumn
>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
></
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
<
asp:SqlDataSource
ID
=
"AdventureWorks"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
SelectCommand="SELECT * FROM Sales.[SalesPerson]"></
asp:SqlDataSource
>
</
div
>
</
form
>
</
body
>
</
html
>
I don't have anything server side.
Thanks.
0

Casey
Top achievements
Rank 1
answered on 05 Mar 2012, 02:51 PM
Hi Jocelyn,
What is the DataType of the SalesYTD column in the Sales.SalesPerson table?
Are you binding the same way in your project where DataFormatString is not working? If so, what is the DataType of your column, in the database, that the DataFormatString property is not working for?
The DataFormatString property wouldn't work for me when the DataType from my DataSource was a string. However, if it was a decimal, it would apply the DataFormatString property.
Casey
What is the DataType of the SalesYTD column in the Sales.SalesPerson table?
Are you binding the same way in your project where DataFormatString is not working? If so, what is the DataType of your column, in the database, that the DataFormatString property is not working for?
The DataFormatString property wouldn't work for me when the DataType from my DataSource was a string. However, if it was a decimal, it would apply the DataFormatString property.
Casey
0

Jocelyn
Top achievements
Rank 1
answered on 05 Mar 2012, 02:55 PM
The SalesYTD DB DataType is "money" and in mine everything is String. I am binding the same way except that I am not using a SqlDataSource. I am using a DataTable filled with alot of SqlRequest.
So I guess I'll have to format in code behind inside ItemDataBound?
Jocelyn
So I guess I'll have to format in code behind inside ItemDataBound?
Jocelyn
0

Jocelyn
Top achievements
Rank 1
answered on 05 Mar 2012, 03:13 PM
Is there a way to auto-convert the data bound to the column so that the DecimalDigit apply to the column?
0
Accepted

Casey
Top achievements
Rank 1
answered on 05 Mar 2012, 03:19 PM
Hi Jocelyn,
I'm not really sure about auto-converting the data bound column to a Decimal Digit.
It looks like you could specify the DataType of the column in your DataTable prior to adding data to it. The following link demonstrates how to add columns of different DataType to a DataTable. Maybe you could try adding a Double, or Decimal column to your DataTable to use for your GridBoundColumn's DataField?
MSDN DataColumn.DataType Property
I hope this helps!
Casey
I'm not really sure about auto-converting the data bound column to a Decimal Digit.
It looks like you could specify the DataType of the column in your DataTable prior to adding data to it. The following link demonstrates how to add columns of different DataType to a DataTable. Maybe you could try adding a Double, or Decimal column to your DataTable to use for your GridBoundColumn's DataField?
MSDN DataColumn.DataType Property
I hope this helps!
Casey