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
- private void dataGridView1_CellValidating(object sender, System.Windows.Forms.DataGridViewCellValidatingEventArgs e)
- {
- try
- {
- if (e.ColumnIndex == 1)
- {
- if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != (Object)"")
- {
- float pom = float.Parse(Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].Value));
- if (Convert.ToSingle(dataGridView1[e.ColumnIndex, e.RowIndex].Value) < 0) throw new FormatException();
- 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)))
- dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.MistyRose;
- else
- dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = SystemColors.Window;
- }
- }
- }
- catch (FormatException fe)
- {
- MessageBox.Show("Something", "Something", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
Expand|Select|Wrap|Line Numbers
- if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != (Object)"")
Can somebody help me because without this I must to color cells on the button_click event which isn't functional.
Thanks!