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

How to make a GridNumericColumn on a grid to accept integer value only

3 Answers 348 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Herman Gouw
Top achievements
Rank 1
Herman Gouw asked on 30 Jun 2009, 03:18 PM
Hi,

I posted this query back in April and then May, and received the following replies:

http://www.telerik.com/community/forums/aspnet/general-discussions/how-to-force-a-gridnumericcolumn-on-a-radgrid-to-accept-integer-values-only.aspx


However, these replies still haven't solved the problem.

I have a grid with 3 columns:
Column 1 - a read only GridBoundColumn which displays a string
Column 2 - an editable GridNumericColumn which displays and accepts an integer value only
Column 3 - an editable GridNumericColumn which displays and accepts an double value only

The editable GridNumericColumn (Columns 2 & 3) will always be in edit mode.
The user can then any value on the editable columns.
However, I am still not able to make Column 2 to accept an integer value only (validation done on the client side) .
Column 3 will only accept a double value only.
When the user clicks the submit button on the page, each value of the editable column will be retrieved and stored on the business object.

Can anybody please help me on how to do this (i.e. to make an editable GridNumericColumn on a grid to accept an integer value)?

You can see the web page on http://www.gouw.ws/radgrid

The code is given below:
ASPX
=====
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="radScriptManager" runat="server" />
        <telerik:RadGrid ID="radGrid" AutoGenerateColumns="false" AllowMultiRowEdit="true" AllowMultiRowSelection="true" GridLines="Vertical" OnItemCreated="radGrid_ItemCreated" OnPreRender="radGrid_PreRender" OnNeedDataSource="radGrid_NeedDataSource" runat="server">
            <ClientSettings>
                <Scrolling UseStaticHeaders="true" />
            </ClientSettings>
            <MasterTableView DataKeyNames="Read" EditMode="InPlace" TableLayout="Fixed">
                <Columns>
                    <telerik:GridBoundColumn DataField="Read" DataType="System.String" HeaderText="Read Only" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" ReadOnly="true" UniqueName="Read" />
                    <telerik:GridNumericColumn DataField="Integer" DataFormatString="{0:N0}" DataType="System.Int32" HeaderText="Integer Only" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" ReadOnly="false" UniqueName="Integer" />
                    <telerik:GridNumericColumn DataField="Dbl" DataFormatString="{0:#,##0.#0}" DataType="System.Double" HeaderText="Double Only" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" ReadOnly="false" UniqueName="Dbl" />
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    <div align="left">
        <asp:Button ID="button" OnClick="button_Click" Text="Submit" runat="server" />
    </div>
    </form>

ASPX.CS
=======
    public partial class Default : System.Web.UI.Page
    {
        private ArrayList _data = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                this._data = (ArrayList)Session["DATA"];
            }
            else
            {
                this._data = Data.Load();
                Session["DATA"] = this._data;
            }
        }

        protected void radGrid_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.IsInEditMode)
            {
                RadNumericTextBox numBox = (e.Item as GridDataItem)["Integer"].Controls[0] as RadNumericTextBox;
                numBox.NumberFormat.DecimalDigits = 0;
                numBox.NumberFormat.AllowRounding = false;
                numBox.NumberFormat.KeepNotRoundedValue = true;
            }
        }

        protected void radGrid_PreRender(object sender, System.EventArgs e)
        {
            foreach (GridDataItem item in this.radGrid.Items)
            {
                item.Edit = true;
            }
            this.radGrid.Rebind();
        }

        protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            this.radGrid.DataSource = this._data;
        }

        protected void button_Click(object sender, EventArgs e)
        {
            // To fetch the editable value on the grid and to store it into the business object
        }
    }

Thanks & regards,
Herman Gouw
Skype: hermangouw

3 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 03 Jul 2009, 09:43 AM
Hello Herman,

You can find attached a sample page I have created for your reference. It demonstrates how RadGrid's ItemCreated event can be used to reference RadNumericTextBox controls in the edited items and set their values, so that integers are rounded to 0 decimal places and doubles are rounded to 4 (or any arbitrary number). Thus, you can enter only integers in the integer column, and all values are converted to double in the double column.

Sincerely yours,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Herman Gouw
Top achievements
Rank 1
answered on 06 Jul 2009, 01:41 AM
Thanks Veli. Your solution works perfectly fine.

One more question ...
When the user enters a blank on Quantity, the value 0 should be automatically displayed instead.
When the user enters a blank on Price, the value 0.0 should be automatically displayed instead.
Is there any properties or methods to do this?
Or do I have to do this using javascript functions?
Can you please show me how to do this?

Best regards,
Herman Gouw
0
Princy
Top achievements
Rank 1
answered on 06 Jul 2009, 08:02 AM
Hello Herman,

You can set the EmptyMessage property of the NumericTextBox in the ItemCreated event as shown below in the code:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    {      
       if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
         { 
            GridEditableItem item = e.Item as GridEditableItem; 
            GridNumericColumnEditor editor = item.EditManager.GetColumnEditor("Price"as GridNumericColumnEditor; 
            editor.NumericTextBox.NumberFormat.AllowRounding = true
            editor.NumericTextBox.NumberFormat.DecimalDigits = 4; 
            editor.NumericTextBox.EmptyMessage = "0.0"
 
            editor = item.EditManager.GetColumnEditor("Quantity"as GridNumericColumnEditor; 
            editor.NumericTextBox.NumberFormat.AllowRounding = true
            editor.NumericTextBox.NumberFormat.DecimalDigits = 0; 
            editor.NumericTextBox.EmptyMessage = "0"
         } 
    }

Thanks
Princy.
Tags
General Discussions
Asked by
Herman Gouw
Top achievements
Rank 1
Answers by
Veli
Telerik team
Herman Gouw
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or