c# - How to prevent bad formatted data input in DataGridViewCell -
I have a Datagrid view that receives the data and data directly from a strongly typed dataset
Updates and its table adapter
.
DataGridView
allows data editing but I am having problems dealing with poorly formatted data input.
For example, there is a date in the column, formatted in the database as datetime
, 11/05/2010 and you can edit the date and DataGridView opens a
TextBox
in which you can enter letters, symbols and other unauthorized characters. When you finish editing the cell, it throws a System.FormatException
with such poor data, how can I enter some data?
Is there a way to "filter" the data that is sent back to the datagrid view?
=========================================== ======================================
As Allen said , The key was to handle the cellwatching event. I will attach some code that helps in handling the value:
public static zero cellillating (object sender, system.independent.form.datgrid view cell validation Accenture e) {string newValue = e.formattedValue.ToString (); String oldValue = ((System.Windows.Forms.DataGridView) (Sender)). Rows [e. Randex]. Seles [E. Column index]. Formatted Values Toasting (); / I want to check that the value has changed before validating (if new!! = Old value) {if (false) // change it with actual verification. {// If the data is not valid then cancel e.Cancel = true; }}}}
There are row / cellwaltting events on the DataGride view, you use these If the input is invalid then validate and cancel the input - it leaves the row / cell in editing mode.
Comments
Post a Comment