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

Number of decimal digits in RadGrid - GridNumericColumn

13 Answers 3342 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 24 Aug 2011, 08:59 AM
Hello,

can you help me find resolve my following problem..?

I have RadGrid with Numeric Columns and one GridButtonColumn for automatic deletes. This is markup of one of my columns:

<telerik:GridNumericColumn DataField="HoursAmount" HeaderText="hrs." DataFormatString="{0:### ##0.0}"
  ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" />

When I click on delete link on column where value is bigger then 1000, grid write this error: Failed to set one or more properties on type Arcon.Web.ServiceJobHour. 1 000,00 is not a valid value for Decimal.

Now I'm finding way to use DecimalDigits property of GridNumericColumn but this doesn't do anything...

<telerik:GridNumericColumn DataField="HoursAmount" HeaderText="hrs." DataFormatString="{0:### ##0.0}"
  ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right"  DecimalDigits="2" />

Can you help me find to resolve it?

Thanks

13 Answers, 1 is accepted

Sort by
0
Pavel
Telerik team
answered on 29 Aug 2011, 09:07 AM
Hello Daniel,

I am attaching a simple test page which works with the DataFormatString you have specified. Let me know what is different in your application.

Greetings,
Pavel
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Daniel
Top achievements
Rank 1
answered on 30 Aug 2011, 12:41 PM
Hello Pavel,

important different between our projects is I'm using LinqDataSource. Date type of my problematic column in DB is Money and in LINQ Data Model is Decimal. Please try connect your radgrid to DB via LINQ and probably you will see same error...

What is DecimalDigits property on GridNumericColumn? I edited your sample for show you why I think about this function. Please look at this...

<%@ Page Language="C#" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    protected void RadGrid1_NeedDataSource(object sender, EventArgs e)
    {
        RadGrid1.DataSource = SampleData;
    }
 
    public DataTable SampleData
    {
        get
        {
            if (Session["_data"] == null || !Page.IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("ID", Type.GetType("System.Int32"));
                table.Columns.Add("Value1", Type.GetType("System.Double"));
                table.Columns.Add("Value2", Type.GetType("System.Double"));
                table.Columns.Add("Value3", Type.GetType("System.Double"));
 
                Random rand = new Random();
 
                for (int i = 0; i < 10; i++)
                {
                    table.Rows.Add(i,
                                   rand.Next(0, 50),
                                   rand.Next(0, 50),
                                   i * 12 * 100.1234);
                }
 
                Session["_data"] = table;
            }
 
            DataTable original = (DataTable)Session["_data"];
            DataTable ret = original.Clone();
            DataRow[] sortedRows = original.Select("", "ID ASC");
            foreach (DataRow row in sortedRows)
            {
                ret.Rows.Add(row.ItemArray);
            }
 
            return ret;
        }
    }
</script>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="SM" runat="server"></asp:ScriptManager>
    <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Telerik"
            AllowPaging="True" AllowSorting="True" PageSize="10" Width="200px" ShowStatusBar="true"
            OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false">
            <MasterTableView DataKeyNames="ID" Width="200px">
               <Columns>
                    <telerik:GridNumericColumn DataField="Value3" HeaderText="hrs." DecimalDigits="2" />
               </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>


Thank You
0
Pavel
Telerik team
answered on 30 Aug 2011, 02:53 PM
Hi Daniel,

I cannot reproduce the problem when binding the Grid to a LinqDataSource as well. As for the DecimalDigits property of the column, it determines the formatting in the input textbox which is generated for this cell, when the item is put in edit mode. It does not affect the formatting of the data in the cell in regular view mode.

Greetings,
Pavel
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Daniel
Top achievements
Rank 1
answered on 31 Aug 2011, 10:54 AM
Ok, thanks...

Can you answer my folowing question?

What is DecimalDigits property on GridNumericColumn? I edited your sample for show you why I think about this function. Please look at this... If I have problem with DataFormatString property, want use DecimalDigits and it doesn't do anything. I send you edited your sample code and it's wrong...

Please look at this and help me.
Thanks
0
Jayesh Goyani
Top achievements
Rank 2
answered on 31 Aug 2011, 11:39 AM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Telerik"
           AllowPaging="True" AllowSorting="True" PageSize="10" Width="200px" ShowStatusBar="true"
           OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false">
           <MasterTableView DataKeyNames="ID" Width="200px">
              <Columns>
                   <telerik:GridNumericColumn DataField="Value3" HeaderText="hrs." DataFormatString="{0:f3}"                    
                     DataType="System.Decimal"  AllowRounding="false"/>
                     <telerik:GridNumericColumn DataField="ID" HeaderText="hrs.ID" DecimalDigits="2"                      
                     DataType="System.Decimal" />
              </Columns>
           </MasterTableView>
       </telerik:RadGrid>

let me know if any concern.

Thanks,
Jayesh Goyani
0
Daniel
Top achievements
Rank 1
answered on 31 Aug 2011, 11:58 AM
Hello Jayesh,

please try to run folowing code and tell how many decimal digits do you see in second columns where in code is set only one decimal digit:

<%@ Page Language="C#" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    protected void RadGrid1_NeedDataSource(object sender, EventArgs e)
    {
        RadGrid1.DataSource = SampleData;
    }
 
    public DataTable SampleData
    {
        get
        {
            if (Session["_data"] == null || !Page.IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("ID", Type.GetType("System.Int32"));
                table.Columns.Add("Value1", Type.GetType("System.Double"));
                table.Columns.Add("Value2", Type.GetType("System.Double"));
                table.Columns.Add("Value3", Type.GetType("System.Double"));
 
                Random rand = new Random();
 
                for (int i = 0; i < 10; i++)
                {
                    table.Rows.Add(i,
                                   rand.Next(0, 50),
                                   rand.Next(0, 50),
                                   i * 12 * 100.123);
                }
 
                Session["_data"] = table;
            }
 
            DataTable original = (DataTable)Session["_data"];
            DataTable ret = original.Clone();
            DataRow[] sortedRows = original.Select("", "ID ASC");
            foreach (DataRow row in sortedRows)
            {
                ret.Rows.Add(row.ItemArray);
            }
 
            return ret;
        }
    }
</script>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="SM" runat="server"></asp:ScriptManager>
    <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Telerik"
            AllowPaging="True" AllowSorting="True" PageSize="10" Width="200px" ShowStatusBar="true"
            OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false" AllowAutomaticDeletes="true">
            <MasterTableView DataKeyNames="ID" Width="200px">
               <Columns>
                    <telerik:GridNumericColumn DataField="Value3" HeaderText="hrs." DataFormatString="{0:### ##0.00}" />
                    <telerik:GridNumericColumn DataField="Value3" HeaderText="hrs." DataType="System.Decimal" DecimalDigits="1" />
                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" ButtonType="LinkButton" />
               </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

Thanks you very much!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 31 Aug 2011, 12:04 PM
Hello,

Right now i got the same issue in "DecimalDigits" so i do your functionality with different way.

Note : DecimalDigits not worked with me also.

Thanks,
Jayesh Goyani
0
Pavel
Telerik team
answered on 31 Aug 2011, 12:28 PM
Hello guys,

As explained in my previous post, the DecimalDigits property is used to control the display of the value in the NumericTextBox control which is generated when the item is in edit mode. It does not affect the formatting of the data in the cell. You should use DataFormatString for this.

Best wishes,
Pavel
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Kavitha
Top achievements
Rank 1
answered on 07 Sep 2012, 04:33 PM

In edit mode, GridNumericColumn does not get RadNumericTextBox as its control. It gets only TextBox control.
For example, here when the grid line is in edit mode, I want to get the RadNumericTextBox of the GridNumericColumn "salary_person".
But I am getting only TextBox control and I am not able to set my groupseparator.

 protected void IGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
  if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
  {
        GridEditableItem editItem = (GridEditableItem)e.Item;
         RadNumericTextBox txtNum = (RadNumericTextBox)editItem[salary_person].Controls[0];
        txtNum.NumberFormat.GroupSeparator = "*";
  }
}

Any ideas?

0
Shinu
Top achievements
Rank 2
answered on 10 Sep 2012, 05:58 AM
Hi Kavitha,

GridNumericColumn in edit mode, it displays a RadNumericTextBox control. This column type is for numeric values. Please check the sample code snippet I tried.

ASPX:
<telerik:GridNumericColumn DataField="Freight" HeaderText="GridNumericColumn" UniqueName="Freight">
</telerik:GridNumericColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        RadNumericTextBox NumericTextBox = (RadNumericTextBox)editItem["Freight"].Controls[0];
        NumericTextBox.NumberFormat.GroupSeparator = "*";
    }
}

Please provide the code if it doesn't help.

Thanks,
Shinu.
0
Kavitha
Top achievements
Rank 1
answered on 10 Sep 2012, 11:12 AM

Hello Shinu,

Yes. GridNumericColumn displays RadNumericTextBox in edit mode.
 I was getting Textbox because template was used. I removed the template and it worked fine.

Thanks for the reply.

Your solution worked in the edit mode. But how to format the number in display mode?

 It displayed my number 123456.78 as 123*456.78 in edit mode. But in display mode, it displays  123456.78 . How can I display with my specific decimal or group separator in display mode?

Thanks in advance.

0
Shinu
Top achievements
Rank 2
answered on 11 Sep 2012, 06:22 AM
Hi Kavitha,

When the grid is in view mode, the GridNumericColumn renders a literal control. To format the data rendered in this case, you can try using the DataFormatString property. I tried with accessing the cell and replacing the ',' with '*'. Here is the sample code snippet.

ASPX:
<telerik:GridNumericColumn DataField="Freight" HeaderText="GridNumericColumn" UniqueName="Freight" DataFormatString="{0:N}"></telerik:GridNumericColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   if (e.Item is GridDataItem)
   {
      GridDataItem ditem = (GridDataItem)e.Item;
      string value = ditem["Freight"].Text;
      if (value.Contains(","))
      {
         value = value.Replace(",", "*");              
      }
      ditem["Freight"].Text = value;
   }
}

Thanks,
Shinu.
0
Kavitha
Top achievements
Rank 1
answered on 11 Sep 2012, 03:30 PM

Hello Shinu,

Your solution worked for me in display mode.

Thanks for the reply.

Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Pavel
Telerik team
Daniel
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Kavitha
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or