473,490 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DataGridView CellValidating event unexplained functionality

1 New Member
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
0 2686

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

Similar topics

9
15380
by: Matt | last post by:
In many places in my application, I have a DataGridView which users can either enter data into or use as a method of navigation in a master/detail form view. I need a method of stopping the user...
3
2027
by: Pieter Coucke | last post by:
Hi, When a user types a non numeric-value in a numeric column in a DataGridView, and he tries to leave the cell, he gets this "Input string was not in a correct format."-exception. Is there a...
2
13300
by: Greg | last post by:
I'm new to the datagridview control, and am left wondering how one goes about restricting input to numeric only. Is it possible to trap key presses at the cell level, and in the relevant columns,...
0
1094
by: Dave | last post by:
I have a datagridview I call the CellValidating event and if the the cell does not validate I want to reset the cell to a default value. My code actually executes this statement: ...
7
16324
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
0
3014
by: Leon_Amirreza | last post by:
Its seems very logical that when a user changes a value in a cell first CellValidating and then CellParsing would get called. But can I cache the value Parsed in CellValidating (To Actually...
5
6327
by: TheSteph | last post by:
Hi ! I have a DataGridView with a Date (DateTime) Column. When a user edit the cell and change the date I woulk like to allow him to write "081501" and programmatically transform the entered...
0
1282
by: Alan Mailer | last post by:
After a user edits a Cell in one of my DataGridViews, I want to make sure what the user has typed in is 'valid'. If it's not, I want to Cancel the Edit. Ok, you say, use the CellValidating...
9
3404
by: Miro | last post by:
My current headache is proper is with the datagridview I am starting to realize that a DataGridView within vs2008 is not as 'robust' as a 'textboxfield' by default for example. Example: A...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.