views:

43

answers:

1

Lets simplify. Lets say I have this class:

class T {
   public string Name { get; set; }
   public int Age { get; set; }
   public int height{ get; set; }
   ...
}

and I have a DataGridView's DataSource bound to a BindingList <T>, with N columns, each one bound to each property.

I need to:

  • Allow the user to enter non validating ages, heights, etc (for instance "aaa")
  • Color the cells with non validating values (red background)
  • Retain the non validating values displayed until the form is closed (I don't want to lose the values entered until the form is closed, so the user has the option to correct the bad cells anytime he wants BEFORE closing the form)
  • Keep the last correct values entered for each cell with non validating values entered.
  • When the form is closed, ditch the non validating values and keep the last correct values entered.

Is there any easy way to do this?

A: 

I can't answer all of it but I can do the red background one.

What you will need to do first of course is handle DataGridView.CellValidating and set e.Cancel to true during the event.

If you want a entire red background instead of a red exclamation point (that is the default behavior) you will need to create a inherited class of DataGridTextBox and override DataGridViewCell.PaintErrorIcon that will allow you to draw the cell red instead of having the red exclamation mark.

Scott Chamberlain