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. DatagridView Cell datatype validation on edit

DatagridView Cell datatype validation on edit

Scheduled Pinned Locked Moved C#
csharp
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.
  • B Offline
    B Offline
    bemahesh
    wrote on last edited by
    #1

    hi, I have a datagridview on windows app(.net 2.0). I wanted to provide user with flexibility of changing any cell values within datagridView. In doing so, i wanted to check if datatype of the cell gets validated. When user updates the value within the datagridView, I want to check the OLD DATATYPE and NEW DATATYPE of the data. Meaning, I want to know beforehand what datatype the column allows and what datatype is the text user typing in. I tried this: object gridCellValue; void repriceQuedataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { gridCellValue = repriceQuedataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; } void repriceQuedataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { Type sourceType = e.ColumnIndex.GetType(); Type changedDataType = e.Value.GetType(); Console.WriteLine(sender.GetType().ToString()); if (!sourceType.Equals(changedDataType)) { MessageBox.Show("Please try again. Expected type is: " + e.ColumnIndex.GetType().ToString()); e.Value = gridCellValue; e.ParsingApplied = true; } } However, Type changedDataType = e.Value.GetType(); line always gets datatype string because e.value is string. I want to check if lets say the original column allows int then the new value that user enters is of type int. I want to write these events in such a way that it is GENERIC FOR ANY DATATYPE. Please shed some light.

    Thanks Needy

    L 1 Reply Last reply
    0
    • B bemahesh

      hi, I have a datagridview on windows app(.net 2.0). I wanted to provide user with flexibility of changing any cell values within datagridView. In doing so, i wanted to check if datatype of the cell gets validated. When user updates the value within the datagridView, I want to check the OLD DATATYPE and NEW DATATYPE of the data. Meaning, I want to know beforehand what datatype the column allows and what datatype is the text user typing in. I tried this: object gridCellValue; void repriceQuedataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { gridCellValue = repriceQuedataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; } void repriceQuedataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { Type sourceType = e.ColumnIndex.GetType(); Type changedDataType = e.Value.GetType(); Console.WriteLine(sender.GetType().ToString()); if (!sourceType.Equals(changedDataType)) { MessageBox.Show("Please try again. Expected type is: " + e.ColumnIndex.GetType().ToString()); e.Value = gridCellValue; e.ParsingApplied = true; } } However, Type changedDataType = e.Value.GetType(); line always gets datatype string because e.value is string. I want to check if lets say the original column allows int then the new value that user enters is of type int. I want to write these events in such a way that it is GENERIC FOR ANY DATATYPE. Please shed some light.

      Thanks Needy

      L Offline
      L Offline
      Lisa Jorgensen
      wrote on last edited by
      #2

      Here is one approach:

      void repriceQuedataGridView1_CellParsing(object sender,
      DataGridViewCellParsingEventArgs e)
      {
      try
      {
      // Validate entered value by converting to column's type. Note that
      // Convert.ChangeType doesn't handle Nullable types. Also, DataGridView
      // doesn't support setting the value to null within the CellParsing
      // event. To work around this requires creating a derived cell that
      // overrides the ParseFormattedValue method. For this example, assume
      // that the user should not enter nulls.

          object convertedValue = Convert.ChangeType(e.Value, e.DesiredType);
      }
      catch
      {
          // Validation failed. Alert user.
      
          MessageBox.Show("Please try again. Expected type is " + 
                          e.DesiredType.ToString());
      
          // The CurrentCell still contains the pre-edit value. If the 
          // previous value was a null, then restoring the null will lead
          // to another exception (because still in CellParsing event). 
          // Attempt to work around the problem by setting the current cell's 
          // value to an empty string. This works for some types (e.g. DateTime) 
          // but not for others (e.g. Double).
      
          DataGridView gridView = (DataGridView)sender;
      
          if (Convert.IsDBNull(gridView.CurrentCell.Value))
          {
              gridView.CurrentCell.Value = string.Empty;
          }
      
          // Set arg value to current cell's value and mark parsing applied.
      
          e.Value = gridView.CurrentCell.Value;
          e.ParsingApplied = true;
      }
      

      }

      Hope this helps.

      B 1 Reply Last reply
      0
      • L Lisa Jorgensen

        Here is one approach:

        void repriceQuedataGridView1_CellParsing(object sender,
        DataGridViewCellParsingEventArgs e)
        {
        try
        {
        // Validate entered value by converting to column's type. Note that
        // Convert.ChangeType doesn't handle Nullable types. Also, DataGridView
        // doesn't support setting the value to null within the CellParsing
        // event. To work around this requires creating a derived cell that
        // overrides the ParseFormattedValue method. For this example, assume
        // that the user should not enter nulls.

            object convertedValue = Convert.ChangeType(e.Value, e.DesiredType);
        }
        catch
        {
            // Validation failed. Alert user.
        
            MessageBox.Show("Please try again. Expected type is " + 
                            e.DesiredType.ToString());
        
            // The CurrentCell still contains the pre-edit value. If the 
            // previous value was a null, then restoring the null will lead
            // to another exception (because still in CellParsing event). 
            // Attempt to work around the problem by setting the current cell's 
            // value to an empty string. This works for some types (e.g. DateTime) 
            // but not for others (e.g. Double).
        
            DataGridView gridView = (DataGridView)sender;
        
            if (Convert.IsDBNull(gridView.CurrentCell.Value))
            {
                gridView.CurrentCell.Value = string.Empty;
            }
        
            // Set arg value to current cell's value and mark parsing applied.
        
            e.Value = gridView.CurrentCell.Value;
            e.ParsingApplied = true;
        }
        

        }

        Hope this helps.

        B Offline
        B Offline
        bemahesh
        wrote on last edited by
        #3

        Thank you Lisa. I will post if any problem occurs. I appreciate your help.

        Thanks Needy

        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