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. Windows Forms
  4. how to validate Gridview cell's text .

how to validate Gridview cell's text .

Scheduled Pinned Locked Moved Windows Forms
helpcsstutorialquestion
3 Posts 2 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.
  • S Offline
    S Offline
    squattyarun
    wrote on last edited by
    #1

    HI, I have a grid control which has three columns viz.quantity, unit price, and total Amount column. Quantity column can have only integer value. I want to validate quantity column in a way so that as soon as user insert value other than integer, a message box should display showing error and return the focus to the cell. OR System should not allow any value other than integer into the cell. Is there any keypress/keydown/keyup event for datagrid's cell also? If yes than please provide me code or any hint... Please help.

    Arun Singh Noida.

    E 1 Reply Last reply
    0
    • S squattyarun

      HI, I have a grid control which has three columns viz.quantity, unit price, and total Amount column. Quantity column can have only integer value. I want to validate quantity column in a way so that as soon as user insert value other than integer, a message box should display showing error and return the focus to the cell. OR System should not allow any value other than integer into the cell. Is there any keypress/keydown/keyup event for datagrid's cell also? If yes than please provide me code or any hint... Please help.

      Arun Singh Noida.

      E Offline
      E Offline
      ElSpinos
      wrote on last edited by
      #2

      You could try the following: 1 - Handle the DataGridView.EditingControlShowing event

      private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
      {
      // Make sure we're getting the key press event for a text type editor...
      if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
      // Handle the editing control's keypress event...
      (e.Control).KeyPress += DataGridViewTextBoxEditingControlTextBoxControl_KeyPress;
      }

      2 - Inside the keypress event, check for the keys you need and process them

      private void DataGridViewTextBoxEditingControlTextBoxControl_KeyPress(object sender, KeyPressEventArgs e)
      {
      // Standard editing keys can be processed...
      if (e.KeyChar == (char)Keys.Back)
      {
      e.Handled = false;
      // No need for further validation...
      return;
      }

      // Just evaluate the contents of the cell if the value type is int
      // NOTE: _dataGridView here is a reference to the grid in context...
      if (_dataGridView.CurrentCell.ValueType != typeof (int))
      return;

      // Validate the key char...
      // NOTE: By specifying e.Handled = true, we are indicating that
      // we absorbed the character and this key is not sent to the cell.
      e.Handled = char.IsNumber(e.KeyChar);
      }

      Hope this helps...

      /F - .NET Developer

      modified on Wednesday, July 30, 2008 11:55 AM

      S 1 Reply Last reply
      0
      • E ElSpinos

        You could try the following: 1 - Handle the DataGridView.EditingControlShowing event

        private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
        // Make sure we're getting the key press event for a text type editor...
        if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
        // Handle the editing control's keypress event...
        (e.Control).KeyPress += DataGridViewTextBoxEditingControlTextBoxControl_KeyPress;
        }

        2 - Inside the keypress event, check for the keys you need and process them

        private void DataGridViewTextBoxEditingControlTextBoxControl_KeyPress(object sender, KeyPressEventArgs e)
        {
        // Standard editing keys can be processed...
        if (e.KeyChar == (char)Keys.Back)
        {
        e.Handled = false;
        // No need for further validation...
        return;
        }

        // Just evaluate the contents of the cell if the value type is int
        // NOTE: _dataGridView here is a reference to the grid in context...
        if (_dataGridView.CurrentCell.ValueType != typeof (int))
        return;

        // Validate the key char...
        // NOTE: By specifying e.Handled = true, we are indicating that
        // we absorbed the character and this key is not sent to the cell.
        e.Handled = char.IsNumber(e.KeyChar);
        }

        Hope this helps...

        /F - .NET Developer

        modified on Wednesday, July 30, 2008 11:55 AM

        S Offline
        S Offline
        squattyarun
        wrote on last edited by
        #3

        Thanx

        Arun Singh INDIA-Noida. My limit is sky and sky is too high.

        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