Cellvalidating in

DataGridView cell validation

2015.08.25 23:49 LordDrakken DataGridView cell validation

In my datagridview I want to add validation to ensure entered values are between a certain numeric range. I'm doing my checking in the CellValidating event. This works out fine:
 private void gvTraining_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { switch (gvTraining.Columns[e.ColumnIndex].HeaderText) { case "NextTrainingLevel": { int newInteger; if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) newInteger <= 0 newInteger >= 21) { gvTraining.Rows[e.RowIndex].ErrorText = "Please enter a numeric value between 0 and 21. Press Esc to clear."; e.Cancel = true; } } break; case "TrainingScore": { int newInteger; if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) newInteger <= 0 newInteger >= 2046820351) { gvTraining.Rows[e.RowIndex].ErrorText = "Please enter a numeric value between 0 and 2046820351. Press Esc to clear."; e.Cancel = true; } } break; case "TrainingLevelComp": { int newInteger; if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) newInteger <= 0 newInteger >= 20) { gvTraining.Rows[e.RowIndex].ErrorText = "Please enter a numeric value between 0 and 20. Press Esc to clear."; e.Cancel = true; } } break; } 
According to what I'm reading online, I also need to use the CellEndEdit event to clear the row error text if the user presses Esc. So I added this and it works fine as well:
 private void gvTraining_CellEndEdit(object sender, DataGridViewCellEventArgs e) { gvTraining.Rows[e.RowIndex].ErrorText = String.Empty; } 
However, it seems that once the cell has entered the error state, the only way to clear that error is to hit Esc. This seems strange. If I see the error and correct it without hitting Esc, it still stays in the error state. I must hit Esc to continue.
Is there a way to do this without needing to hit Esc after failing validation?
submitted by LordDrakken to csharp [link] [comments]


c# Datagridview cell validation + combobox selected change ... GridView - Part 6: Validating Cells (Silverlight & WPF) Getting Cell value from dataGridView using CellClick Event Validation with RadGridView for WinForms C# WPF Tutorial 17- Show database values in Table or ...

  1. c# Datagridview cell validation + combobox selected change ...
  2. GridView - Part 6: Validating Cells (Silverlight & WPF)
  3. Getting Cell value from dataGridView using CellClick Event
  4. Validation with RadGridView for WinForms
  5. C# WPF Tutorial 17- Show database values in Table or ...

In this video, you will learn how to use the event-based Validation functionality in RadGridView for WinForms. Learn how to use the CellValidating and RowValidating events to ensure user input is ... In this Windows form tutorial, I will teach you How to get the cell value from dataGridView using c#. In this tutorial, i has an EDMX file. Apply LINQ expression on EDMX and get data from cell ... C# - How to get user entered data from datagridview to windows Insert, Update and Delete from DataGridView to sqlite WPF ,MySql Sql database WPF WPF. ★★★Top ... Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube. It is hard to write a LOB application that does not have any data validation. In this video, we will walk you through adding data validation for your grid's cells. This allows you to take complete ...