Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. only Number in datagrid cell

only Number in datagrid cell

Scheduled Pinned Locked Moved C#
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mostafa_h
    wrote on last edited by
    #1

    hi , How can i use in a DataGrid Cell Only Number , Iuse yhis function for this :

    public bool OnlyIsNumeric(string val)
    {
    try{
    int result = 0;
    return int.TryParse(val, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result);
    }
    catch{ return false;}
    }

    but Ican not use this in datagrid in DataGrid1_CurrentCellChanged Event : DataGrid1[DataGrid1.CurrentRowIndex, 2] = "";:confused: thanx

    s_mostafa_h

    G 2 Replies Last reply
    0
    • M mostafa_h

      hi , How can i use in a DataGrid Cell Only Number , Iuse yhis function for this :

      public bool OnlyIsNumeric(string val)
      {
      try{
      int result = 0;
      return int.TryParse(val, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result);
      }
      catch{ return false;}
      }

      but Ican not use this in datagrid in DataGrid1_CurrentCellChanged Event : DataGrid1[DataGrid1.CurrentRowIndex, 2] = "";:confused: thanx

      s_mostafa_h

      G Offline
      G Offline
      Gopal S
      wrote on last edited by
      #2

      Hi Mostafa, Please use this code: private void dgview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { dgview.EditingControl.KeyPress += new KeyPressEventHandler(dgviewKeyPress); } private void dgviewKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar)) args.Handled = true; } Please let me know if this helps. Thanks,

      Gopal.S

      M 1 Reply Last reply
      0
      • G Gopal S

        Hi Mostafa, Please use this code: private void dgview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { dgview.EditingControl.KeyPress += new KeyPressEventHandler(dgviewKeyPress); } private void dgviewKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar)) args.Handled = true; } Please let me know if this helps. Thanks,

        Gopal.S

        M Offline
        M Offline
        mostafa_h
        wrote on last edited by
        #3

        thanks a lot for ur reply , but If I want to this (only number ) Only in Column 2 ( one of my field is numeric for e.g. SerialNumber ), in this case How can i do this . By the way I have DataGrid , Not DataGridView .then this object has not the properties that u wrote . :-O thanks again -- modified at 6:58 Monday 7th May, 2007

        s_mostafa_h

        A 1 Reply Last reply
        0
        • M mostafa_h

          thanks a lot for ur reply , but If I want to this (only number ) Only in Column 2 ( one of my field is numeric for e.g. SerialNumber ), in this case How can i do this . By the way I have DataGrid , Not DataGridView .then this object has not the properties that u wrote . :-O thanks again -- modified at 6:58 Monday 7th May, 2007

          s_mostafa_h

          A Offline
          A Offline
          AFSEKI
          wrote on last edited by
          #4

          Use DataGrid.EndEdit method to validate if the entered value is numeric or not. Have a look at MSDN Library DataGrid.EndEdit where you can find an example code. Hope this helps.

          1 Reply Last reply
          0
          • M mostafa_h

            hi , How can i use in a DataGrid Cell Only Number , Iuse yhis function for this :

            public bool OnlyIsNumeric(string val)
            {
            try{
            int result = 0;
            return int.TryParse(val, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result);
            }
            catch{ return false;}
            }

            but Ican not use this in datagrid in DataGrid1_CurrentCellChanged Event : DataGrid1[DataGrid1.CurrentRowIndex, 2] = "";:confused: thanx

            s_mostafa_h

            G Offline
            G Offline
            Gopal S
            wrote on last edited by
            #5

            Hi Mustafa, Here is the another solution: namespace Sample { public partial class Form1 : Form { DataGrid dGird; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dGird = new DataGrid(); dGird.CurrentCellChanged += new EventHandler(dGridCellChanged); DataTable dt = new DataTable(); dt.Columns.Add("SNo"); dt.Columns.Add("Name"); dt.Columns.Add("Amount"); dt.Columns.Add("Address"); DataRow drow = dt.NewRow(); drow[0] = "01"; drow[1] = "Gopal"; drow[2] = "100"; drow[3] = "TTL"; dt.Rows.Add(drow); drow = dt.NewRow(); drow[0] = "02"; drow[1] = "Raja"; drow[2] = "150"; drow[3] = "SSS"; dt.Rows.Add(drow); dGird.DataSource = dt; dGird.Size = new Size(400, 400); this.Controls.Add(dGird); System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); } private void dgKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar) && args.KeyChar!=(char)Keys.Back) args.Handled = true; } private void dGridCellChanged(object sender, EventArgs args) { // Control index - 0,1 assigned for scrollbars(default). System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); else if (dGird.CurrentCell.ColumnNumber == 2) ctlCollection[4].KeyPress += new KeyPressEventHandler(dgKeyPress); } } } please let me know if this helps. Thanks,

            Gopal.S

            M 1 Reply Last reply
            0
            • G Gopal S

              Hi Mustafa, Here is the another solution: namespace Sample { public partial class Form1 : Form { DataGrid dGird; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dGird = new DataGrid(); dGird.CurrentCellChanged += new EventHandler(dGridCellChanged); DataTable dt = new DataTable(); dt.Columns.Add("SNo"); dt.Columns.Add("Name"); dt.Columns.Add("Amount"); dt.Columns.Add("Address"); DataRow drow = dt.NewRow(); drow[0] = "01"; drow[1] = "Gopal"; drow[2] = "100"; drow[3] = "TTL"; dt.Rows.Add(drow); drow = dt.NewRow(); drow[0] = "02"; drow[1] = "Raja"; drow[2] = "150"; drow[3] = "SSS"; dt.Rows.Add(drow); dGird.DataSource = dt; dGird.Size = new Size(400, 400); this.Controls.Add(dGird); System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); } private void dgKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar) && args.KeyChar!=(char)Keys.Back) args.Handled = true; } private void dGridCellChanged(object sender, EventArgs args) { // Control index - 0,1 assigned for scrollbars(default). System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); else if (dGird.CurrentCell.ColumnNumber == 2) ctlCollection[4].KeyPress += new KeyPressEventHandler(dgKeyPress); } } } please let me know if this helps. Thanks,

              Gopal.S

              M Offline
              M Offline
              mostafa_h
              wrote on last edited by
              #6

              it's help me , thanks in advance ! Yours ,;)

              s_mostafa_h

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups