DatagridView Cell datatype validation on edit
-
hi, I have a datagridview on windows app(.net 2.0). I wanted to provide user with flexibility of changing any cell values within datagridView. In doing so, i wanted to check if datatype of the cell gets validated. When user updates the value within the datagridView, I want to check the OLD DATATYPE and NEW DATATYPE of the data. Meaning, I want to know beforehand what datatype the column allows and what datatype is the text user typing in. I tried this: object gridCellValue; void repriceQuedataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { gridCellValue = repriceQuedataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; } void repriceQuedataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { Type sourceType = e.ColumnIndex.GetType(); Type changedDataType = e.Value.GetType(); Console.WriteLine(sender.GetType().ToString()); if (!sourceType.Equals(changedDataType)) { MessageBox.Show("Please try again. Expected type is: " + e.ColumnIndex.GetType().ToString()); e.Value = gridCellValue; e.ParsingApplied = true; } } However, Type changedDataType = e.Value.GetType(); line always gets datatype string because e.value is string. I want to check if lets say the original column allows int then the new value that user enters is of type int. I want to write these events in such a way that it is GENERIC FOR ANY DATATYPE. Please shed some light.
Thanks Needy
-
hi, I have a datagridview on windows app(.net 2.0). I wanted to provide user with flexibility of changing any cell values within datagridView. In doing so, i wanted to check if datatype of the cell gets validated. When user updates the value within the datagridView, I want to check the OLD DATATYPE and NEW DATATYPE of the data. Meaning, I want to know beforehand what datatype the column allows and what datatype is the text user typing in. I tried this: object gridCellValue; void repriceQuedataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { gridCellValue = repriceQuedataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; } void repriceQuedataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { Type sourceType = e.ColumnIndex.GetType(); Type changedDataType = e.Value.GetType(); Console.WriteLine(sender.GetType().ToString()); if (!sourceType.Equals(changedDataType)) { MessageBox.Show("Please try again. Expected type is: " + e.ColumnIndex.GetType().ToString()); e.Value = gridCellValue; e.ParsingApplied = true; } } However, Type changedDataType = e.Value.GetType(); line always gets datatype string because e.value is string. I want to check if lets say the original column allows int then the new value that user enters is of type int. I want to write these events in such a way that it is GENERIC FOR ANY DATATYPE. Please shed some light.
Thanks Needy
Here is one approach:
void repriceQuedataGridView1_CellParsing(object sender,
DataGridViewCellParsingEventArgs e)
{
try
{
// Validate entered value by converting to column's type. Note that
// Convert.ChangeType doesn't handle Nullable types. Also, DataGridView
// doesn't support setting the value to null within the CellParsing
// event. To work around this requires creating a derived cell that
// overrides the ParseFormattedValue method. For this example, assume
// that the user should not enter nulls.object convertedValue = Convert.ChangeType(e.Value, e.DesiredType); } catch { // Validation failed. Alert user. MessageBox.Show("Please try again. Expected type is " + e.DesiredType.ToString()); // The CurrentCell still contains the pre-edit value. If the // previous value was a null, then restoring the null will lead // to another exception (because still in CellParsing event). // Attempt to work around the problem by setting the current cell's // value to an empty string. This works for some types (e.g. DateTime) // but not for others (e.g. Double). DataGridView gridView = (DataGridView)sender; if (Convert.IsDBNull(gridView.CurrentCell.Value)) { gridView.CurrentCell.Value = string.Empty; } // Set arg value to current cell's value and mark parsing applied. e.Value = gridView.CurrentCell.Value; e.ParsingApplied = true; }
}
Hope this helps.
-
Here is one approach:
void repriceQuedataGridView1_CellParsing(object sender,
DataGridViewCellParsingEventArgs e)
{
try
{
// Validate entered value by converting to column's type. Note that
// Convert.ChangeType doesn't handle Nullable types. Also, DataGridView
// doesn't support setting the value to null within the CellParsing
// event. To work around this requires creating a derived cell that
// overrides the ParseFormattedValue method. For this example, assume
// that the user should not enter nulls.object convertedValue = Convert.ChangeType(e.Value, e.DesiredType); } catch { // Validation failed. Alert user. MessageBox.Show("Please try again. Expected type is " + e.DesiredType.ToString()); // The CurrentCell still contains the pre-edit value. If the // previous value was a null, then restoring the null will lead // to another exception (because still in CellParsing event). // Attempt to work around the problem by setting the current cell's // value to an empty string. This works for some types (e.g. DateTime) // but not for others (e.g. Double). DataGridView gridView = (DataGridView)sender; if (Convert.IsDBNull(gridView.CurrentCell.Value)) { gridView.CurrentCell.Value = string.Empty; } // Set arg value to current cell's value and mark parsing applied. e.Value = gridView.CurrentCell.Value; e.ParsingApplied = true; }
}
Hope this helps.