Numeric only cell

8 posts, 2 answers
  1. Matt
    Matt avatar
    11 posts
    Member since:
    May 2009

    Posted 04 Apr 2011 Link to this post

    Hi,

    I have need to have columns in a gridview numeric only, BUT I have to have the cell blank when the field is null...

    I have tried using GridViewDecimalColumn but when it's blank and the CellValueChanged event fires I get FormatException "Input string was not in a correct format."

    And, of course, if I use a GridViewTextBoxColumn it allows the user to type in letters...

    Can anyome suggest how I can get this to work? Not allow letters, but also allow empty columns?

    Thanks,

    Matt


    Oh, I am using Q3 2010
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Apr 2011 Link to this post

    Hi Matt,

    This should be fine as long as your object allows null values. Please see the exmaple below

    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;
      
    namespace RadControlsWinFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
      
                List<MyObject> myObjectList = new List<MyObject>();
                myObjectList.Add(new MyObject(null, "String 1"));
                myObjectList.Add(new MyObject(1, "String 2"));
                myObjectList.Add(new MyObject(2, "String 3"));
                myObjectList.Add(new MyObject(null, "String 4"));
      
                this.radGridView1.AutoGenerateColumns = false;
                GridViewDecimalColumn decimalColumn = new GridViewDecimalColumn();
                decimalColumn.Name = "DecimalColumn";
                decimalColumn.HeaderText = "My Decimal Column";
                decimalColumn.FieldName = "MyNumber";
                decimalColumn.DecimalPlaces = 0;
                radGridView1.MasterTemplate.Columns.Add(decimalColumn);
      
                GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn();
                textColumn.Name = "TextColumn";
                textColumn.HeaderText = "My string Column";
                textColumn.FieldName = "MyString";
                radGridView1.MasterTemplate.Columns.Add(textColumn);
      
                this.radGridView1.DataSource = myObjectList;
            }
        }
      
        public class MyObject
        {
            public MyObject(int? myNumber, string myText)
            { this.MyNumber = myNumber; this.MyString = myText; }
      
            public int? MyNumber
            { get; set; }
      
            public string MyString
            { get; set; }
        }
    }

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Apr 2011 Link to this post

    Hello guys,

    Or you can use a textbox column and on the EditorRequired event you can just change the type of the editor to spin editor, like so:
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (radGridView1.CurrentCell.ColumnInfo.Name == "Name")
        {
            e.EditorType = typeof(GridSpinEditor);
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  5. Matt
    Matt avatar
    11 posts
    Member since:
    May 2009

    Posted 07 Apr 2011 Link to this post

    Hi Guys,

    Thanks for the replies..

    I'm trying Emanuel's solution and almost have it working.. the only thing not working now is when I click in the cell, it changes to teh spin editor like it should, but the initial value is "100".. How do I get it to start with the existing cell value?

    I tried:
        e.EditorType = typeof(GridSpinEditor);
        e.Editor.Value = gvMain.CurrentCell.Value;

    But I get a NullReference exception on setting the .Value..

    ... On second look, I am still getting "Input string was not in a correct format."  when a 'blank' cell tryies to open as a spin editor..

    Thanks,

    Matt
  6. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Apr 2011 Link to this post

    Hello again,

    For this please use the CellEditorInitialized event, and there set the value.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP

  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 07 Apr 2011 Link to this post

    Hello Matt,

    Here is an example for you.

    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
      
        public partial class Form1 : Form
        {
            private BindingList<Person> people = new BindingList<Person>();
            private RadGridView radGridView1;
      
            public Form1()
            {
                radGridView1 = new RadGridView();
                radGridView1.Dock = DockStyle.Fill;
                this.Controls.Add(radGridView1);
      
                radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
                  
      
                InitializeComponent();
                people.Add(new Person("1", "Richard"));
                people.Add(new Person("2", "Chris"));
                people.Add(new Person(null, "Bob"));
      
                radGridView1.AutoGenerateColumns = false;
      
                GridViewTextBoxColumn idColumn = new GridViewTextBoxColumn();
                idColumn.FieldName = "Id";
                idColumn.HeaderText = "Id";
      
                GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn();
                nameColumn.FieldName = "Name";
                nameColumn.HeaderText = "Name";
      
                radGridView1.Columns.Add(idColumn);
                radGridView1.Columns.Add(nameColumn);
      
                radGridView1.DataSource = people;
            }
      
            void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
            {
                if (radGridView1.CurrentCell.ColumnInfo.Name == "Id")
                {
                    e.EditorType = typeof(GridSpinEditor);
                }
            }
        }
      
        public class Person
        {
            public Person(string id, string name)
            {
                this.Name = name;
                this.Id = id;
            }
      
            public string Name
            { get; set; }
      
            public string Id
            { get; set; }
        }

    Hope that helps
    Richard
  8. Rawad
    Rawad avatar
    23 posts
    Member since:
    Jan 2012

    Posted 08 Jul 2014 Link to this post

    Hello 

    I tried Richard Slade method, but I'm able to enter alphanumeric in the field Id, and When I enter alphanumeric "Test" it's giving me: " Input string was not in a correct format."
    How to not let user enter alphanumeric, and there's is solution to prevent user to enter alphanumeric for specific cells, and not for all the column,  lets say :  Row(0) Cell(0) --> User can edit text,  but  Row(1) Cell(0) -->  User edit integer,   Row(2) Cell(0)--> user edit decimal, 
    Row (3) Cell(0) --> User edit a text , . . .

    Thanks
  9. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 11 Jul 2014 Link to this post

    Hi Rawad,

    I have answered this question in the other forum thread you have posted: http://www.telerik.com/forums/validate-cell-datagrid#Y7UscmlzkUuoKs96Bm-cKA. Please avoid posting the same question numerous time. If you cannot find a suitable thread to address your question, you can always open a new one.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready