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

Numeric only cell

7 Answers 555 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 04 Apr 2011, 07:31 PM
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

7 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 05 Apr 2011, 02:09 PM
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
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Apr 2011, 08:53 PM
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
0
Matt
Top achievements
Rank 1
answered on 07 Apr 2011, 03:38 PM
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
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 07 Apr 2011, 03:40 PM
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

0
Richard Slade
Top achievements
Rank 2
answered on 07 Apr 2011, 03:46 PM

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
0
Rawad
Top achievements
Rank 2
answered on 08 Jul 2014, 07:28 AM
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
0
Stefan
Telerik team
answered on 11 Jul 2014, 06:49 AM
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.
 
Tags
GridView
Asked by
Matt
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Emanuel Varga
Top achievements
Rank 1
Matt
Top achievements
Rank 1
Rawad
Top achievements
Rank 2
Stefan
Telerik team
Share this question
or