protected void WriteValidationCodeDetailsGrid() { bool validationsEnabled = ValidationsEnabled; if (!validationsEnabled) { return; } List <ColumnValidation> validationColumns = GetValidationColumns(); Writer.PushIdentationLevel(); Writer.IncreaseIdentation(); Writer.IncreaseIdentation(); Writer.WriteLine("private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)"); Writer.WriteLine("{"); Writer.WriteLine("string s;"); Writer.WriteLine("DataGridViewRow row = dataGridView1.Rows[e.RowIndex];"); Writer.WriteLine("object value = e.FormattedValue;"); Writer.WriteLine("e.Cancel = false;"); Writer.WriteLine("row.ErrorText = \"\";"); Writer.WriteLine("if (row.IsNewRow) return;"); for (int i = 0; i < validationColumns.Count; i++) { ColumnValidation cv = validationColumns[i]; if (cv.IsBooleanType() || cv.HasLookup) { continue; } string idColumnCanonical = GetCanonicalIdentifier(cv.Column.ColumnName); Writer.WriteLine("if (e.ColumnIndex == {0})", i); Writer.WriteLine("{"); string numericType = ""; if (cv.IsNumericType()) { if (cv.IsIntegerType()) { numericType = "int"; } if (cv.IsFloatingPointType()) { numericType = "double"; } Writer.WriteLine("{0} v;", numericType); } if (cv.Required) { Writer.WriteLine("if( (value is DBNull) || string.IsNullOrEmpty( value.ToString() ) )"); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("row.ErrorText = \"The field {0} is required\";", cv.Name); Writer.WriteLine("return;"); Writer.WriteLine("}"); } if (cv.IsNumericType()) { Writer.WriteLine("s = value.ToString();"); if (!string.IsNullOrEmpty(numericType)) { Writer.WriteLine("if( !{0}.TryParse( s, out v ) )", numericType); Writer.WriteLine("{"); } else { // just assume is good Writer.WriteLine("if( true )"); Writer.WriteLine("{"); } Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("row.ErrorText = \"The field {0} must be {1}.\";", cv.Name, numericType); Writer.WriteLine("return;"); Writer.WriteLine("}"); if (cv.MinValue != null) { Writer.WriteLine("else if( {0} > v )", cv.MinValue); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("row.ErrorText = \"The field {0} must be greater or equal than {1}.\";", cv.Name, cv.MinValue); Writer.WriteLine("return;"); Writer.WriteLine("} "); } if (cv.MaxValue != null) { Writer.WriteLine("else if( {0} < v )", cv.MaxValue); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("row.ErrorText = \"The field {0} must be lesser or equal than {1}\";", cv.Name, cv.MaxValue); Writer.WriteLine("return;"); Writer.WriteLine("} "); } } Writer.WriteLine("}"); } Writer.WriteLine("}"); // DataError event Writer.WriteLine(""); Writer.WriteLine("private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)"); Writer.WriteLine("{"); Writer.WriteLine("dataGridView1.Rows[e.RowIndex].ErrorText = e.Exception.Message;"); Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("}"); Writer.PopIdentationLevel(); }
protected void WriteValidationCodeDetailsGrid() { bool validationsEnabled = ValidationsEnabled; if (!validationsEnabled) { return; } List <ColumnValidation> validationColumns = GetValidationColumns(); Writer.PushIdentationLevel(); Writer.IncreaseIdentation(); Writer.IncreaseIdentation(); Writer.WriteLine("Private Sub dataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs)"); Writer.WriteLine(""); Writer.WriteLine("Dim s As String"); Writer.WriteLine("Dim row As DataGridViewRow= dataGridView1.Rows(e.RowIndex)"); Writer.WriteLine("Dim value As Object = e.FormattedValue"); Writer.WriteLine("e.Cancel = False"); Writer.WriteLine("row.ErrorText = \"\""); Writer.WriteLine("If row.IsNewRow Then"); Writer.WriteLine("Return"); Writer.WriteLine("End If"); for (int i = 0; i < validationColumns.Count; i++) { ColumnValidation cv = validationColumns[i]; if (cv.IsBooleanType() || cv.HasLookup) { continue; } string idColumnCanonical = GetCanonicalIdentifier(cv.Column.ColumnName); Writer.WriteLine("If e.ColumnIndex = {0} Then", i); Writer.WriteLine(""); string numericType = ""; if (cv.IsNumericType()) { if (cv.IsIntegerType()) { numericType = "Integer"; } if (cv.IsFloatingPointType()) { numericType = "Double"; } Writer.WriteLine("Dim v as {0}", numericType); } if (cv.Required) { Writer.WriteLine("If (TypeOf value is DBNull) OrElse String.IsNullOrEmpty( value.ToString() ) Then "); Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("row.ErrorText = \"The field {0} is required\"", cv.Name); Writer.WriteLine("Return"); Writer.WriteLine("End If"); } if (cv.IsNumericType()) { Writer.WriteLine("s = value.ToString()"); if (!string.IsNullOrEmpty(numericType)) { Writer.WriteLine("If Not {0}.TryParse( s, v ) Then", numericType); } else { // just assume is good Writer.WriteLine("If True Then"); } Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("row.ErrorText = \"The field {0} must be {1}.\"", cv.Name, numericType); Writer.WriteLine("Return"); if (cv.MinValue != null) { Writer.WriteLine("ElseIf {0} > v Then", cv.MinValue); Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("row.ErrorText = \"The field {0} must be greater or equal than {1}.\"", cv.Name, cv.MinValue); Writer.WriteLine("Return"); } if (cv.MaxValue != null) { Writer.WriteLine("ElseIf {0} < v Then", cv.MaxValue); Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("row.ErrorText = \"The field {0} must be lesser or equal than {1}\"", cv.Name, cv.MaxValue); Writer.WriteLine("Return"); } Writer.WriteLine("End If"); } Writer.WriteLine("End If"); } Writer.WriteLine("End Sub"); // DataError Writer.WriteLine("Private Sub dataGridView1_DataError(ByVal sender As Object, ByVal e As DataGridViewDataErrorEventArgs)"); Writer.WriteLine("dataGridView1.Rows(e.RowIndex).ErrorText = e.Exception.Message"); Writer.WriteLine("e.Cancel = true"); Writer.WriteLine("End Sub"); Writer.PopIdentationLevel(); }
protected override void WriteValidationCode() { bool validationsEnabled = ValidationsEnabled; List <ColumnValidation> validationColumns = ValidationColumns; Writer.PushIdentationLevel(); Writer.IncreaseIdentation(); Writer.IncreaseIdentation(); if (validationsEnabled) { for (int i = 0; i < validationColumns.Count; i++) { ColumnValidation cv = validationColumns[i]; if (cv.IsDateType() || cv.IsReadOnly() || cv.IsBooleanType()) { continue; } string idColumnCanonical = GetCanonicalIdentifier(cv.Column.ColumnName); if (cv.HasLookup && cv.Required) { Writer.WriteLine("private void {0}_comboBox_Validating(object sender, CancelEventArgs e)", idColumnCanonical); Writer.WriteLine("{"); Writer.WriteLine("int i = {0}_comboBox.SelectedIndex;", idColumnCanonical); Writer.WriteLine("e.Cancel = false;"); Writer.WriteLine("if( i == -1 )"); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = true; "); Writer.WriteLine("errorProvider1.SetError({0}_comboBox, \"Must select a {0}\");", idColumnCanonical); Writer.WriteLine("}"); Writer.WriteLine("if( !e.Cancel )"); Writer.WriteLine("{"); Writer.WriteLine("errorProvider1.SetError({0}_comboBox, \"\");", idColumnCanonical); Writer.WriteLine("}"); Writer.WriteLine("}"); continue; } Writer.WriteLine("private void {0}TextBox_Validating(object sender, CancelEventArgs e)", idColumnCanonical); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = false;"); if (cv.Required) { Writer.WriteLine("if( string.IsNullOrEmpty( {0}TextBox.Text ) )", idColumnCanonical); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} is required\" ); ", idColumnCanonical, cv.Name); Writer.WriteLine("}"); } if (cv.IsNumericType()) { string numericType = ""; if (cv.IsIntegerType()) { numericType = "int"; } else if (cv.IsFloatingPointType()) { numericType = "double"; } Writer.WriteLine("{0} v;", numericType); Writer.WriteLine("string s = {0}TextBox.Text;", idColumnCanonical); if (!string.IsNullOrEmpty(numericType)) { Writer.WriteLine("if( !{0}.TryParse( s, out v ) )", numericType); Writer.WriteLine("{"); } else { // just assume is good Writer.WriteLine("if( true )"); Writer.WriteLine("{"); } Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} must be {2}.\" );", idColumnCanonical, cv.Name, numericType); Writer.WriteLine("}"); if (cv.MinValue != null) { Writer.WriteLine("else if( {0} > v )", cv.MinValue); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} must be greater or equal than {2}.\" );", idColumnCanonical, cv.Name, cv.MinValue); Writer.WriteLine("}"); } if (cv.MaxValue != null) { Writer.WriteLine("else if( {0} < v )", cv.MaxValue); Writer.WriteLine("{"); Writer.WriteLine("e.Cancel = true;"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} must be lesser or equal than {2}\" );", idColumnCanonical, cv.Name, cv.MaxValue); Writer.WriteLine("} "); } } Writer.WriteLine("if( !e.Cancel ) {{ errorProvider1.SetError( {0}TextBox, \"\" ); }} ", idColumnCanonical); Writer.WriteLine("}"); Writer.WriteLine(); } } Writer.PopIdentationLevel(); }
protected override void WriteValidationCode() { bool validationsEnabled = ValidationsEnabled; List <ColumnValidation> validationColumns = ValidationColumns; Writer.PushIdentationLevel(); Writer.IncreaseIdentation(); Writer.IncreaseIdentation(); if (validationsEnabled) { for (int i = 0; i < validationColumns.Count; i++) { ColumnValidation cv = validationColumns[i]; if (cv.IsDateType() || cv.IsReadOnly() || cv.IsBooleanType()) { continue; } string idColumnCanonical = GetCanonicalIdentifier(cv.Column.ColumnName); if (cv.HasLookup && cv.Required) { Writer.WriteLine("Private Sub {0}_comboBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs)", idColumnCanonical); Writer.WriteLine("Dim i As Integer = {0}_comboBox.SelectedIndex", idColumnCanonical); Writer.WriteLine("e.Cancel = False"); Writer.WriteLine("If i = -1 Then"); Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("errorProvider1.SetError({0}_comboBox, \"Must select a {0}\")", idColumnCanonical); Writer.WriteLine("End If"); Writer.WriteLine("If Not e.Cancel Then"); Writer.WriteLine("errorProvider1.SetError({0}_comboBox, \"\")", idColumnCanonical); Writer.WriteLine("End If"); Writer.WriteLine("End Sub"); continue; } Writer.WriteLine("Private Sub {0}TextBox_Validating(sender As Object, e As CancelEventArgs)", idColumnCanonical); Writer.WriteLine(""); Writer.WriteLine("e.Cancel = False"); if (cv.Required) { Writer.WriteLine("If String.IsNullOrEmpty( {0}TextBox.Text ) Then ", idColumnCanonical); Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} is required\" ) ", idColumnCanonical, cv.Name); Writer.WriteLine("End If"); } if (cv.IsNumericType()) { string numericType = ""; if (cv.IsIntegerType()) { numericType = "Integer"; } else if (cv.IsFloatingPointType()) { numericType = "Double"; } Writer.WriteLine("Dim v As {0}", numericType); Writer.WriteLine("Dim s As string = {0}TextBox.Text", idColumnCanonical); if (!string.IsNullOrEmpty(numericType)) { Writer.WriteLine("If Not {0}.TryParse( s, v ) Then", numericType); } else { // just assume is good Writer.WriteLine("If True Then"); } Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} must be {2}.\" )", idColumnCanonical, cv.Name, numericType); if (cv.MinValue != null) { Writer.WriteLine("ElseIf {0} > v Then ", cv.MinValue); Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} must be greater or equal than {2}.\" )", idColumnCanonical, cv.Name, cv.MinValue); } if (cv.MaxValue != null) { Writer.WriteLine("ElseIf {0} < v Then ", cv.MaxValue); Writer.WriteLine("e.Cancel = True"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"The field {1} must be lesser or equal than {2}\" )", idColumnCanonical, cv.Name, cv.MaxValue); } Writer.WriteLine("End If"); } Writer.WriteLine("If Not e.Cancel Then"); Writer.WriteLine("errorProvider1.SetError( {0}TextBox, \"\" )", idColumnCanonical); Writer.WriteLine("End If"); Writer.WriteLine("End Sub"); Writer.WriteLine(); } } Writer.PopIdentationLevel(); }