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

Numeric Gridview

3 Answers 191 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Guna
Top achievements
Rank 1
Guna asked on 03 Feb 2011, 12:16 PM
How to display the numeric datagridview , (ie) all rows and columns must accept only numeric values .. and to mask the cells in this format - 0000.00

3 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 03 Feb 2011, 02:15 PM
helo Guna,

There may be other ways to do this using Custom editors but this may be a quick and simple way of doing what you need. Please can you try the below

Designer File
namespace RadGridView_Basic_C
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            this.radSpinEditor1 = new Telerik.WinControls.UI.RadSpinEditor();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radSpinEditor1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(457, 510);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized);
            // 
            // radSpinEditor1
            // 
            this.radSpinEditor1.Location = new System.Drawing.Point(196, 517);
            this.radSpinEditor1.Maximum = new decimal(new int[] {
            999999,
            0,
            0,
            131072});
            this.radSpinEditor1.Name = "radSpinEditor1";
            // 
            // 
            // 
            this.radSpinEditor1.RootElement.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.WrapAroundChildren;
            this.radSpinEditor1.ShowBorder = true;
            this.radSpinEditor1.Size = new System.Drawing.Size(100, 21);
            this.radSpinEditor1.TabIndex = 1;
            this.radSpinEditor1.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(457, 556);
            this.Controls.Add(this.radSpinEditor1);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radSpinEditor1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
        private Telerik.WinControls.UI.RadSpinEditor radSpinEditor1;
    }
}

Form1.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using System.Globalization;
  
  
namespace RadGridView_Basic_C
{
    public partial class Form1 : Form
    {
  
        private List<MyNumericType> m_myList = new List<MyNumericType>();
  
  
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
  
            this.radGridView1.AutoGenerateColumns = false;
  
            m_myList.Add(new MyNumericType(10, 1));
            m_myList.Add(new MyNumericType(20, 2));
            m_myList.Add(new MyNumericType(30, 3));
            m_myList.Add(new MyNumericType(40, 0));
            radGridView1.DataSource = m_myList;
  
            GridViewDecimalColumn int1Column = new GridViewDecimalColumn();
            int1Column.Name = "Int1";
            int1Column.HeaderText = "Int1";
            int1Column.FieldName = "Int1";
            int1Column.DecimalPlaces = 2;
            int1Column.Minimum = Convert.ToDecimal(0000.00);
            int1Column.Maximum = new decimal(new int[] { 999999, 0, 0, 131072 });
            this.radGridView1.Columns.Add(int1Column);
  
            GridViewDecimalColumn int2Column = new GridViewDecimalColumn();
            int2Column.Name = "Int2";
            int2Column.HeaderText = "Int2";
            int2Column.FieldName = "Int2";
            int2Column.DecimalPlaces = 2;
            int2Column.Minimum = Convert.ToDecimal(0000.00);
            int2Column.Maximum = new decimal(new int[] { 999999, 0, 0, 131072 });
            this.radGridView1.Columns.Add(int2Column);
  
  
            this.radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(this.radGridView1_CellFormatting);
            this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized);
        }
  
  
        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement is GridDataCellElement)
            {
                if (e.CellElement.Value != null)
                {
                    bool notHasDecimalPlaces = (Convert.ToDecimal(e.CellElement.Value) % 1 == 0);
                    string text = e.CellElement.Value.ToString();
                    char pad = '0';
                    if (notHasDecimalPlaces)
                    {
                        text = text.PadLeft(4, pad);
                        e.CellElement.Text = text + ".00";  
                    }
                    else
                    {
                        text = text.PadLeft(7, pad);
                        e.CellElement.Text = text;  
                    }               
                }
            }
        }
  
        void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            GridSpinEditor spinEditor = this.radGridView1.ActiveEditor as GridSpinEditor;
            if (spinEditor != null)
            {
                spinEditor.DecimalPlaces = 2;
                spinEditor.MinValue = 0;
                spinEditor.MaxValue = new decimal(new int[] { 999999, 0, 0, 131072 });
                if (spinEditor.Value == null)
                {
                    spinEditor.Value = 0000.000;
                }
            }
        }
  
  
        class MyNumericType
        {
            public MyNumericType()
            { }
  
            public MyNumericType(decimal int1, decimal int2)
            {
                this.Int1 = int1;
                this.Int2 = int2;
            }
  
            public decimal Int1 { get; set; }
            public decimal Int2 { get; set; }
  
        }
  
    }
  
 }

Hope that helps
Richard
0
Guna
Top achievements
Rank 1
answered on 04 Feb 2011, 06:01 AM
Hi Richard, 

    This is the Exact Coding which i am looking for ...
     thnaks a lot
0
Richard Slade
Top achievements
Rank 2
answered on 04 Feb 2011, 07:51 AM
No problem. Please remember to mark as answer. Richard.
Tags
GridView
Asked by
Guna
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Guna
Top achievements
Rank 1
Share this question
or