By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

DataGridView CellValidating event unexplained functionality

P: 1
Hello,


I have one little problem which make me crazy. I've made application (for Windows) in Visual C#. On one panel I've got datagridview control which is not bind to database, I populate it manualy. It has 4 columns and only one can be edited and user can put some numbers in it (It is float type). I want to validate ie. to check if that value is in some boundaries (min and max value is in 2. and 3. column and column for editing is 1.). If value is out of that min-max range back color of that cell should change when I leave it. Nothing else. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. private void dataGridView1_CellValidating(object sender, System.Windows.Forms.DataGridViewCellValidatingEventArgs e)
  2.         {
  3.             try
  4.             {
  5.  
  6.                 if (e.ColumnIndex == 1)
  7.                 {
  8.  
  9.                     if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != (Object)"")
  10.                     {
  11.  
  12.                         float pom = float.Parse(Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].Value));
  13.                         if (Convert.ToSingle(dataGridView1[e.ColumnIndex, e.RowIndex].Value) < 0) throw new FormatException();
  14.  
  15.                         if ((Convert.ToSingle(dataGridView1[e.ColumnIndex, e.RowIndex].Value) < Convert.ToSingle(dataGridView1[2, e.RowIndex].Value)) | (Convert.ToSingle(dataGridView1[e.ColumnIndex, e.RowIndex].Value) > Convert.ToSingle(dataGridView1[3, e.RowIndex].Value)))
  16.                             dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.MistyRose;
  17.                         else
  18.                             dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = SystemColors.Window;
  19.                     }
  20.  
  21.                 }
  22.  
  23.             }
  24.             catch (FormatException fe)
  25.             {
  26.                 MessageBox.Show("Something", "Something", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  27.             }
  28.  
  29.  
  30.         }
My problem is that when first time I entering numbers in cell and I go to other cell ie. cell loses focus, cell validating event is fired, but the value of that cell I left is empty string, "". It isn't that that I typed. If I, after that, reenter same cell and again get out from it, only then the value of cell will be adequate and the back color of the cell would change if it is out of range. And the reason for this is not in the line:
Expand|Select|Wrap|Line Numbers
  1.  if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != (Object)"") 
because the same is happening without it, and I put it in the code to avoid showing messagebox from catch segment every first time when I leaving the cell. If there isn't that line float.Parse or Convert.tosingle will raise FormatException event in every case.

Can somebody help me because without this I must to color cells on the button_click event which isn't functional.

Thanks!
Jul 14 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.