/// <summary>
 /// Raises the AfterControlValidation event.
 /// </summary>
 /// <param name="e">Data that contains the binder's binded field validation event information.</param>
 protected virtual void OnAfterControlValidation(DataBinderValidationEventArgs e)
 {
     if (AfterControlValidation != null) AfterControlValidation(this, e);
 }
        private DataBinderValidationEventArgs ValidateGrid(object grid)
        {
            if (grid != null)
            {
                DataBinderValidationEventArgs _args = new DataBinderValidationEventArgs((Control) grid);
                string _notification = ""; object _datasource = null;
                
                if (Materia.PropertyExists(grid, "DataSource"))
                {
                    try { _datasource = Materia.GetPropertyValue(grid, "DataSource"); }
                    catch { _datasource = null; }
                }

                if (_datasource != null)
                {
                    DataBinding _binding = ValidationGridBinding(grid);
                    if (_binding != null)
                    {
                        bool _addnew = true;
                        if (Materia.PropertyExists(grid, "AllowAddNew"))
                        {
                            _addnew = Materia.GetPropertyValue<bool>(grid, "AllowAddNew", false);
                            Materia.SetPropertyValue(grid, "AllowAddNew", false);
                        }

                        if (Materia.PropertyExists(grid, "Rows") &&
                            Materia.PropertyExists(grid, "Cols"))
                        {
                            object _rows = Materia.GetPropertyValue(grid, "Rows");
                            int _rowcount = Materia.GetPropertyValue<int>(_rows, "Count");
                            int _rowfixed = Materia.GetPropertyValue<int>(_rows, "Fixed");

                            if (_rowcount == _rowfixed) _notification = "Please specify at least an entry detail.";

                            if (String.IsNullOrEmpty(_notification.RLTrim()))
                            {
                                DataTable _table = null;

                                try { _table = (DataTable)_datasource; }
                                catch { _table = null; }

                                if (_table != null)
                                {
                                    object _cols = null;

                                    try { _cols = Materia.GetPropertyValue(grid, "Cols"); }
                                    catch { _cols = null; }

                                    if (_cols != null)
                                    {
                                        foreach (DataColumn _column in _table.Columns)
                                        {
                                            if (_binding.RequiredFields.Contains(_column.ColumnName))
                                            {
                                                object _col = null;

                                                try { _col = Materia.GetPropertyValue<object>(_cols, "Item", new object[] { _column.ColumnName}, null); }
                                                catch { _col = null; }

                                                if (_col != null)
                                                {
                                                    object _coleditor = Materia.GetPropertyValue(_col, "Editor");
                                                    string _colcaption = Materia.GetPropertyValue<string>(_col, "Caption", "");

                                                    if (_column.DataType.Name == typeof(string).Name ||
                                                        _column.DataType.Name == typeof(String).Name)
                                                    {
                                                        if (_coleditor != null)
                                                        {
                                                            object _editordatasource = null;

                                                            try { _editordatasource = Materia.GetPropertyValue(_coleditor, "DataSource"); }
                                                            catch { _editordatasource = null; }

                                                            if (_editordatasource != null)
                                                            {
                                                                for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                {
                                                                    object _row = null;

                                                                    try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null ); }
                                                                    catch { _row = null; }

                                                                    if (_row != null)
                                                                    {
                                                                        object _value = null;

                                                                        try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                        catch { _value = null; }

                                                                        if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                        else
                                                                        {
                                                                            if (String.IsNullOrEmpty(_value.ToString().RLTrim())) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                string _displaymember = "";

                                                                                try { _displaymember = Materia.GetPropertyValue<string>(_coleditor, "DisplayMember", ""); }
                                                                                catch { _displaymember = ""; }

                                                                                if (!String.IsNullOrEmpty(_displaymember.RLTrim()))
                                                                                {
                                                                                    DataTable _coleditortable = null;
                                                                                    try { _coleditortable = (DataTable)_editordatasource; }
                                                                                    catch { _coleditortable = null; }

                                                                                    if (_coleditortable != null)
                                                                                    {
                                                                                        object _selvalue = _coleditortable.GetValue<object>("(CONVERT([" + _displaymember + "], System.String) LIKE '" + _value.ToString().ToSqlValidString(true) + "')", _displaymember);
                                                                                        if (Materia.IsNullOrNothing(_selvalue)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }

                                                                    if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                    {
                                                                        int _subindex = _rowfixed - 1;
                                                                        if (_subindex <= 0) _subindex = 0;

                                                                        try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                        catch { }

                                                                        int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                        try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                        catch { }

                                                                        _notification = String.Format(_notification, i - _subindex); break;
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (Materia.MethodExists(_coleditor, "FindStringExact"))
                                                                {
                                                                    for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                    {
                                                                        object _row = null;

                                                                        try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                        catch { _row = null; }

                                                                        if (_row != null)
                                                                        {
                                                                            object _value = null;

                                                                            try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                            catch { _value = null; }

                                                                            if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                if (String.IsNullOrEmpty(_value.ToString().RLTrim())) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                else
                                                                                {
                                                                                    int _index = Materia.GetMethodValue<int>(_coleditor, "FindStringExact", new object[] { _value.ToString() }, -1);
                                                                                    if (_index < 0) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                }
                                                                            }
                                                                        }

                                                                        if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                        {
                                                                            int _subindex = _rowfixed - 1;
                                                                            if (_subindex <= 0) _subindex = 0;

                                                                            try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                            catch { }

                                                                            int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                            try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                            catch { }

                                                                            _notification = String.Format(_notification, i - _subindex); break;
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                    {
                                                                        object _row = null;

                                                                        try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i } ,null); }
                                                                        catch { _row = null; }

                                                                        if (_row != null)
                                                                        {
                                                                            object _value = null;

                                                                            try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                            catch { _value = null; }

                                                                            if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                if (String.IsNullOrEmpty(_value.ToString().RLTrim())) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            }
                                                                        }

                                                                        if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                        {
                                                                            int _subindex = _rowfixed - 1;
                                                                            if (_subindex <= 0) _subindex = 0;

                                                                            try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                            catch { }

                                                                            int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                            try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                            catch { }

                                                                            _notification = String.Format(_notification, i - _subindex); break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                            {
                                                                object _row = null;

                                                                try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                catch { _row = null; }

                                                                if (_row != null)
                                                                {
                                                                    object _value = null;

                                                                    try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName}, null); }
                                                                    catch { _value = null; }

                                                                    if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                    else
                                                                    {
                                                                        if (String.IsNullOrEmpty(_value.ToString().RLTrim())) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                    }
                                                                }

                                                                if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                {
                                                                    int _subindex = _rowfixed - 1;
                                                                    if (_subindex <= 0) _subindex = 0;

                                                                    try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                    catch { }

                                                                    int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                    try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                    catch { }

                                                                    _notification = String.Format(_notification, i - _subindex); break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else if (_column.DataType.Name == typeof(DateTime).Name)
                                                    {
                                                        if (_coleditor != null)
                                                        {
                                                            object _editordatasource = null;

                                                            try { _editordatasource = Materia.GetPropertyValue(_coleditor, "DataSource"); }
                                                            catch { _editordatasource = null; }

                                                            if (_editordatasource != null)
                                                            {
                                                                for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                {
                                                                    object _row = null;

                                                                    try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                    catch { _row = null; }

                                                                    if (_row != null)
                                                                    {
                                                                        object _value = null;

                                                                        try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                        catch { _value = null; }

                                                                        if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                        else
                                                                        {
                                                                            if (!VisualBasic.IsDate(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                string _displaymember = "";

                                                                                try { _displaymember = Materia.GetPropertyValue<string>(_coleditor, "DisplayMember", ""); }
                                                                                catch { _displaymember = ""; }

                                                                                if (!String.IsNullOrEmpty(_displaymember.RLTrim()))
                                                                                {
                                                                                    DataTable _coleditortable = null;
                                                                                    try { _coleditortable = (DataTable)_editordatasource; }
                                                                                    catch { _coleditortable = null; }

                                                                                    if (_coleditortable != null)
                                                                                    {
                                                                                        object _selvalue = _coleditortable.GetValue<object>("(CONVERT([" + _displaymember + "], System.String) LIKE '" + _value.ToString().ToSqlValidString(true) + "')", _displaymember);
                                                                                        if (Materia.IsNullOrNothing(_selvalue)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }

                                                                    if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                    {
                                                                        int _subindex = _rowfixed - 1;
                                                                        if (_subindex <= 0) _subindex = 0;

                                                                        try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                        catch { }

                                                                        int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                        try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                        catch { }

                                                                        _notification = String.Format(_notification, i - _subindex); break;
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (Materia.MethodExists(_coleditor, "FindStringExact"))
                                                                {
                                                                    for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                    {
                                                                        object _row = null;

                                                                        try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                        catch { _row = null; }

                                                                        if (_row != null)
                                                                        {
                                                                            object _value = null;

                                                                            try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                            catch { _value = null; }

                                                                            if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                if (!VisualBasic.IsDate(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                else
                                                                                {
                                                                                    int _index = Materia.GetMethodValue<int>(_coleditor, "FindStringExact", new object[] { _value.ToString() }, -1);
                                                                                    if (_index < 0) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                }
                                                                            }
                                                                        }

                                                                        if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                        {
                                                                            int _subindex = _rowfixed - 1;
                                                                            if (_subindex <= 0) _subindex = 0;

                                                                            try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                            catch { }

                                                                            int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                            try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                            catch { }

                                                                            _notification = String.Format(_notification, i - _subindex); break;
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                    {
                                                                        object _row = null;

                                                                        try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                        catch { _row = null; }

                                                                        if (_row != null)
                                                                        {
                                                                            object _value = null;

                                                                            try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] {_column.ColumnName }, null); }
                                                                            catch { _value = null; }

                                                                            if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                if (!VisualBasic.IsDate(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            }
                                                                        }

                                                                        if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                        {
                                                                            int _subindex = _rowfixed - 1;
                                                                            if (_subindex <= 0) _subindex = 0;

                                                                            try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                            catch { }

                                                                            int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                            try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                            catch { }

                                                                            _notification = String.Format(_notification, i - _subindex); break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                            {
                                                                object _row = null;

                                                                try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                catch { _row = null; }

                                                                if (_row != null)
                                                                {
                                                                    object _value = null;

                                                                    try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName } ,null); }
                                                                    catch { _value = null; }

                                                                    if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                    else
                                                                    {
                                                                        if (!VisualBasic.IsDate(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                    }
                                                                }

                                                                if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                {
                                                                    int _subindex = _rowfixed - 1;
                                                                    if (_subindex <= 0) _subindex = 0;

                                                                    try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                    catch { }

                                                                    int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                    try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                    catch { }

                                                                    _notification = String.Format(_notification, i - _subindex); break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else if (_column.DataType.Name == typeof(byte).Name ||
                                                             _column.DataType.Name == typeof(Byte).Name ||
                                                             _column.DataType.Name == typeof(decimal).Name ||
                                                             _column.DataType.Name == typeof(Decimal).Name ||
                                                             _column.DataType.Name == typeof(double).Name ||
                                                             _column.DataType.Name == typeof(Double).Name ||
                                                             _column.DataType.Name == typeof(float).Name ||
                                                             _column.DataType.Name == typeof(int).Name ||
                                                             _column.DataType.Name == typeof(Int16).Name ||
                                                             _column.DataType.Name == typeof(Int32).Name ||
                                                             _column.DataType.Name == typeof(Int64).Name ||
                                                             _column.DataType.Name == typeof(long).Name ||
                                                             _column.DataType.Name == typeof(sbyte).Name ||
                                                             _column.DataType.Name == typeof(SByte).Name ||
                                                             _column.DataType.Name == typeof(short).Name ||
                                                             _column.DataType.Name == typeof(Single).Name)
                                                    {
                                                        if (_coleditor != null)
                                                        {
                                                            object _editordatasource = null;

                                                            try { _editordatasource = Materia.GetPropertyValue(_coleditor, "DataSource"); }
                                                            catch { _editordatasource = null; }

                                                            if (_editordatasource != null)
                                                            {
                                                                for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                {
                                                                    object _row = null;

                                                                    try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                    catch { _row = null; }

                                                                    if (_row != null)
                                                                    {
                                                                        object _value = null;

                                                                        try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName } , null); }
                                                                        catch { _value = null; }

                                                                        if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                        else
                                                                        {
                                                                            if (!VisualBasic.IsDate(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                string _displaymember = "";

                                                                                try { _displaymember = Materia.GetPropertyValue<string>(_coleditor, "DisplayMember", ""); }
                                                                                catch { _displaymember = ""; }

                                                                                if (!String.IsNullOrEmpty(_displaymember.RLTrim()))
                                                                                {
                                                                                    DataTable _coleditortable = null;
                                                                                    try { _coleditortable = (DataTable)_editordatasource; }
                                                                                    catch { _coleditortable = null; }

                                                                                    if (_coleditortable != null)
                                                                                    {
                                                                                        object _selvalue = _coleditortable.GetValue<object>("(CONVERT([" + _displaymember + "], System.String) LIKE '" + _value.ToString().ToSqlValidString(true) + "')", _displaymember);
                                                                                        if (Materia.IsNullOrNothing(_selvalue)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }

                                                                    if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                    {
                                                                        int _subindex = _rowfixed - 1;
                                                                        if (_subindex <= 0) _subindex = 0;

                                                                        try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                        catch { }

                                                                        int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                        try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                        catch { }

                                                                        _notification = String.Format(_notification, i - _subindex); break;
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (Materia.MethodExists(_coleditor, "FindStringExact"))
                                                                {
                                                                    for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                    {
                                                                        object _row = null;

                                                                        try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                        catch { _row = null; }

                                                                        if (_row != null)
                                                                        {
                                                                            object _value = null;

                                                                            try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                            catch { _value = null; }

                                                                            if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                if (!VisualBasic.IsDate(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                else
                                                                                {
                                                                                    int _index = Materia.GetMethodValue<int>(_coleditor, "FindStringExact", new object[] { _value.ToString() }, -1);
                                                                                    if (_index < 0) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                }
                                                                            }
                                                                        }

                                                                        if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                        {
                                                                            int _subindex = _rowfixed - 1;
                                                                            if (_subindex <= 0) _subindex = 0;

                                                                            try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                            catch { }

                                                                            int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                            try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                            catch { }

                                                                            _notification = String.Format(_notification, i - _subindex); break;
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                                    {
                                                                        object _row = null;

                                                                        try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                        catch { _row = null; }

                                                                        if (_row != null)
                                                                        {
                                                                            object _value = null;

                                                                            try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                            catch { _value = null; }

                                                                            if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                            else
                                                                            {
                                                                                if (!VisualBasic.IsNumeric(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                                else
                                                                                {
                                                                                    if (VisualBasic.CDbl(_value) == 0) _notification = "Please specify a proper value for " + _colcaption + " at row {0}.";
                                                                                }
                                                                            }
                                                                        }

                                                                        if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                        {
                                                                            int _subindex = _rowfixed - 1;
                                                                            if (_subindex <= 0) _subindex = 0;

                                                                            try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                            catch { }

                                                                            int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                            try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                            catch { }

                                                                            _notification = String.Format(_notification, i - _subindex); break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            for (int i = _rowfixed; i <= (_rowcount - 1); i++)
                                                            {
                                                                object _row = null;

                                                                try { _row = Materia.GetPropertyValue<object>(_rows, "Item", new object[] { i }, null); }
                                                                catch { _row = null; }

                                                                if (_row != null)
                                                                {
                                                                    object _value = null;

                                                                    try { _value = Materia.GetPropertyValue<object>(_row, "Item", new object[] { _column.ColumnName }, null); }
                                                                    catch { _value = null; }

                                                                    if (Materia.IsNullOrNothing(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                    else
                                                                    {
                                                                        if (!VisualBasic.IsNumeric(_value)) _notification = "Please specify a value for " + _colcaption + " at row {0}.";
                                                                        else
                                                                        {
                                                                            if (VisualBasic.CDbl(_value) == 0) _notification = "Please specify a proper value for " + _colcaption + " at row {0}.";
                                                                        }
                                                                    }
                                                                }

                                                                if (!String.IsNullOrEmpty(_notification.RLTrim()))
                                                                {
                                                                    int _subindex = _rowfixed - 1;
                                                                    if (_subindex <= 0) _subindex = 0;

                                                                    try { Materia.SetPropertyValue(grid, "Row", i); }
                                                                    catch { }

                                                                    int _colfixed = Materia.GetPropertyValue<int>(_cols, "Fixed", 1);

                                                                    try { Materia.SetPropertyValue(grid, "Col", _column.Ordinal + _colfixed); }
                                                                    catch { }

                                                                    _notification = String.Format(_notification, i - _subindex); break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                            if (!String.IsNullOrEmpty(_notification.RLTrim())) break;
                                        }
                                    }
                                }
                            }
                        }

                        if (Materia.PropertyExists(grid, "AllowAddNew"))
                        {
                            try { Materia.SetPropertyValue(grid, "AllowAddNew", _addnew); }
                            catch { }
                        }

                        if (!String.IsNullOrEmpty(_notification.RLTrim())) _args = new DataBinderValidationEventArgs(grid, false);
                        else
                        {
                            if (_args == null) _args = new DataBinderValidationEventArgs(grid);
                        }

                        _args.Notification = _notification;

                        return _args;
                    }
                    else return null;
                }
                else return null;
            }
            else return null;
        }
 /// <summary>
 /// Raises the BeforeValidation event.
 /// </summary>
 /// <param name="e">Data that contains the binder's validation event information.</param>
 protected virtual void OnBeforeValidation(DataBinderValidationEventArgs e)
 {
     if (BeforeValidation != null) BeforeValidation(this, e);
 }
        private DataBinderValidationEventArgs ValidateControls(object control)
        {
            if (control != null)
            {
                DataBinderValidationEventArgs _args = new DataBinderValidationEventArgs((Control) control);

                if (control.GetType().Name.ToLower().Contains("C1FlexGrid".ToLower()) ||
                    control.GetType().BaseType.Name.ToLower().Contains("C1FlexGrid".ToLower())) _args = ValidateGrid(control);
                else
                {
                    if (BindedControls.Contains(control))
                    {
                        string _validation = ""; string _field = BindedControls[control].FieldName;
                        DataTable _table = ValidationDataSource(_field);

                        if (_table != null)
                        {
                            DataColumn _column = _table.Columns[_field];

                            if (_column.DataType.Name == typeof(string).Name ||
                                _column.DataType.Name == typeof(String).Name)
                            {
                                if (Materia.PropertyExists(control, "DataSource"))
                                {
                                    object _datasource = Materia.GetPropertyValue(control, "DataSource");
                                    if (_datasource != null)
                                    {
                                        if (Materia.PropertyExists(control, "SelectedValue"))
                                        {
                                            object _selectedvalue = Materia.GetPropertyValue(control, "SelectedValue");
                                            if (Materia.IsNullOrNothing(_selectedvalue)) _validation = "Please select a proper value for this field.";
                                        }
                                        else
                                        {
                                            if (Materia.PropertyExists(control, "SelectedIndex"))
                                            {
                                                int _selectedindex = Materia.GetPropertyValue<int>(control, "SelectedIndex", -1);
                                                if (_selectedindex < 0) _validation = "Please select a proper value for this field.";
                                            }
                                            else
                                            {
                                                if (Materia.PropertyExists(control, "Text"))
                                                {
                                                    string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                                    if (String.IsNullOrEmpty(_text.RLTrim())) _validation = "Please specify a proper value for this field.";
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (Materia.PropertyExists(control, "SelectedIndex"))
                                        {
                                            int _selectedindex = Materia.GetPropertyValue<int>(control, "SelectedIndex", -1);
                                            if (_selectedindex < 0) _validation = "Please select a proper value for this field.";
                                        }
                                        else
                                        {
                                            if (Materia.PropertyExists(control, "Text"))
                                            {
                                                string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                                if (String.IsNullOrEmpty(_text.RLTrim())) _validation = "Please specify a proper value for this field.";
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (Materia.PropertyExists(control, "SelectedIndex"))
                                    {
                                        int _selectedindex = Materia.GetPropertyValue<int>(control, "SelectedIndex", -1);
                                        if (_selectedindex < 0) _validation = "Please select a proper value for this field.";
                                    }
                                    else
                                    {
                                        if (Materia.PropertyExists(control, "Text"))
                                        {
                                            string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                            if (String.IsNullOrEmpty(_text.RLTrim())) _validation = "Please specify a proper value for this field.";
                                        }
                                    }
                                }
                            }
                            else if (_column.DataType.Name == typeof(DateTime).Name)
                            {
                                if (Materia.PropertyExists(control, "DataSource"))
                                {
                                    object _datasource = Materia.GetPropertyValue(control, "DataSource");
                                    if (_datasource != null)
                                    {
                                        if (Materia.PropertyExists(control, "SelectedValue"))
                                        {
                                            object _selectedvalue = Materia.GetPropertyValue(control, "SelectedValue");
                                            if (!VisualBasic.IsDate(_selectedvalue)) _validation = "Please select a proper value for this field.";
                                        }
                                        else
                                        {
                                            if (Materia.PropertyExists(control, "Value"))
                                            {
                                                object _value = Materia.GetPropertyValue(control, "Value");
                                                if (!VisualBasic.IsDate(_value)) _validation = "Please specify a proper value for this field.";
                                            }
                                            else
                                            {
                                                if (Materia.PropertyExists(control, "Text"))
                                                {
                                                    string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                                    if (!VisualBasic.IsDate(_text)) _validation = "Please specify a proper value for this field.";
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (Materia.PropertyExists(control, "Value"))
                                        {
                                            object _value = Materia.GetPropertyValue(control, "Value");
                                            if (!VisualBasic.IsDate(_value)) _validation = "Please specify a proper value for this field.";
                                        }
                                        else
                                        {
                                            if (Materia.PropertyExists(control, "Text"))
                                            {
                                                string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                                if (!VisualBasic.IsDate(_text)) _validation = "Please specify a proper value for this field.";
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (Materia.PropertyExists(control, "Value"))
                                    {
                                        object _value = Materia.GetPropertyValue(control, "Value");
                                        if (!VisualBasic.IsDate(_value)) _validation = "Please specify a proper value for this field.";
                                    }
                                    else
                                    {
                                        if (Materia.PropertyExists(control, "Text"))
                                        {
                                            string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                            if (!VisualBasic.IsDate(_text)) _validation = "Please specify a proper value for this field.";
                                        }
                                    }
                                }
                            }
                            else if (_column.DataType.Name == typeof(byte).Name ||
                                     _column.DataType.Name == typeof(Byte).Name ||
                                     _column.DataType.Name == typeof(decimal).Name ||
                                     _column.DataType.Name == typeof(Decimal).Name ||
                                     _column.DataType.Name == typeof(double).Name ||
                                     _column.DataType.Name == typeof(Double).Name ||
                                     _column.DataType.Name == typeof(float).Name ||
                                     _column.DataType.Name == typeof(int).Name ||
                                     _column.DataType.Name == typeof(Int16).Name ||
                                     _column.DataType.Name == typeof(Int32).Name ||
                                     _column.DataType.Name == typeof(Int64).Name ||
                                     _column.DataType.Name == typeof(long).Name ||
                                     _column.DataType.Name == typeof(sbyte).Name ||
                                     _column.DataType.Name == typeof(SByte).Name ||
                                     _column.DataType.Name == typeof(short).Name ||
                                     _column.DataType.Name == typeof(Single).Name)
                            {
                                if (Materia.PropertyExists(control, "DataSource"))
                                {
                                    object _datasource = Materia.GetPropertyValue(control, "DataSource");
                                    if (_datasource != null)
                                    {
                                        if (Materia.PropertyExists(control, "SelectedValue"))
                                        {
                                            object _selectedvalue = Materia.GetPropertyValue(control, "SelectedValue");
                                            if (!VisualBasic.IsNumeric(_selectedvalue)) _validation = "Please select a proper value for this field.";
                                        }
                                        else
                                        {
                                            if (Materia.PropertyExists(control, "Value"))
                                            {
                                                object _selectedvalue = Materia.GetPropertyValue(control, "Value");
                                                if (!VisualBasic.IsNumeric(_selectedvalue)) _validation = "Please specify a proper value for this field.";
                                                else
                                                {
                                                    if (VisualBasic.CDbl(_selectedvalue) <= 0) _validation = "Please specify a proper value for this field.";
                                                }
                                            }
                                            else
                                            {
                                                if (Materia.PropertyExists(control, "SelectedIndex"))
                                                {
                                                    int _selectedindex = Materia.GetPropertyValue<int>(control, "SelectedIndex", -1);
                                                    if (_selectedindex < 0) _validation = "Please select a proper value for this field.";
                                                }
                                                else
                                                {
                                                    if (Materia.PropertyExists(control, "Text"))
                                                    {
                                                        string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                                        if (!VisualBasic.IsNumeric(_text)) _validation = "Please specify a proper value for this field.";
                                                        else
                                                        {
                                                            if (VisualBasic.CDbl(_text) <= 0) _validation = "Please specify a proper value for this field.";
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (Materia.PropertyExists(control, "Value"))
                                        {
                                            object _selectedvalue = Materia.GetPropertyValue(control, "Value");
                                            if (!VisualBasic.IsNumeric(_selectedvalue)) _validation = "Please specify a proper value for this field.";
                                            else
                                            {
                                                if (VisualBasic.CDbl(_selectedvalue) <= 0) _validation = "Please specify a proper value for this field.";
                                            }
                                        }
                                        else
                                        {
                                            if (Materia.PropertyExists(control, "SelectedIndex"))
                                            {
                                                int _selectedindex = Materia.GetPropertyValue<int>(control, "SelectedIndex", -1);
                                                if (_selectedindex < 0) _validation = "Please select a proper value for this field.";
                                            }
                                            else
                                            {
                                                if (Materia.PropertyExists(control, "Text"))
                                                {
                                                    string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                                    if (!VisualBasic.IsNumeric(_text)) _validation = "Please specify a proper value for this field.";
                                                    else
                                                    {
                                                        if (VisualBasic.CDbl(_text) <= 0) _validation = "Please specify a proper value for this field.";
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (Materia.PropertyExists(control, "Value"))
                                    {
                                        object _selectedvalue = Materia.GetPropertyValue(control, "Value");
                                        if (!VisualBasic.IsNumeric(_selectedvalue)) _validation = "Please specify a proper value for this field.";
                                        else
                                        {
                                            if (VisualBasic.CDbl(_selectedvalue) <= 0) _validation = "Please specify a proper value for this field.";
                                        }
                                    }
                                    else
                                    {
                                        if (Materia.PropertyExists(control, "SelectedIndex"))
                                        {
                                            int _selectedindex = Materia.GetPropertyValue<int>(control, "SelectedIndex", -1);
                                            if (_selectedindex < 0) _validation = "Please select a proper value for this field.";
                                        }
                                        else
                                        {
                                            if (Materia.PropertyExists(control, "Text"))
                                            {
                                                string _text = Materia.GetPropertyValue<string>(control, "Text", "");
                                                if (!VisualBasic.IsNumeric(_text)) _validation = "Please specify a proper value for this field.";
                                                else
                                                {
                                                    if (VisualBasic.CDbl(_text) <= 0) _validation = "Please specify a proper value for this field.";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (_column.DataType.Name.ToLower().Contains("byte[]") ||
                                    _column.DataType.Name.ToLower().Contains("byte()") ||
                                    _column.DataType.Name.ToLower().Contains("bytes[]") ||
                                    _column.DataType.Name.ToLower().Contains("bytes()"))
                                {
                                    if (Materia.PropertyExists(control, "Image"))
                                    {
                                        object _image = Materia.GetPropertyValue(control, "Image");
                                        if (Materia.IsNullOrNothing(_image)) _validation = "Please specify a value for this field.";
                                    }
                                }
                            }
                        }

                        if (!String.IsNullOrEmpty(_validation.RLTrim())) _args = new DataBinderValidationEventArgs((Control) control, false);
                        _args.Notification = _validation;
                    }
                }

                return _args;
            }
            else return null;
        }
        private DataBinderValidationEventArgs Validate(DataBinding binding, DataBinderValidationEventArgs args)
        {
            DataBinderValidationEventArgs _args = null;

            if (binding != null)
            {
                if (binding.Grid != null)
                {
                    _args = new DataBinderValidationEventArgs(binding.Grid);
                    OnBeforeControlValidation(_args);
                    _args = ValidateGrid(binding.Grid);
                    OnAfterControlValidation(_args);
                }

                if (_args == null)
                {
                    if (binding.Details.Count > 0)
                    {
                        foreach (DataBinding db in binding.Details)
                        {
                            _args = Validate(db, _args);
                            if (_args != null)
                            {
                                if (_args.Cancel) break;
                            }
                        }
                    }
                }
                else
                {
                    if (!_args.Cancel)
                    {
                        if (binding.Details.Count > 0)
                        {
                            foreach (DataBinding db in binding.Details)
                            {
                                _args = Validate(db, _args);
                                if (_args != null)
                                {
                                    if (_args.Cancel) break;
                                }
                            }
                        }
                    }
                }
            }

            return _args;
        }
        private DataBinderValidationEventArgs Validate()
        {
            DataBinderValidationEventArgs _args = new DataBinderValidationEventArgs();
            OnBeforeValidation(_args);

            if (!_args.Cancel)
            {
                Form _form = null;

                try { _form = (Form)ContainerControl; }
                catch { }

                foreach (BindedControl bc in BindedControls)
                {
                    object _control = bc.Control;
                    if (_control != null)
                    {
                        _args = null; Materia.RefreshAndManageCurrentProcess(); 
                        _args = new DataBinderValidationEventArgs(_control);
                        OnBeforeControlValidation(_args);
                        _args = null; Materia.RefreshAndManageCurrentProcess();
                        _args = ValidateControls(_control);
                        OnDataValidating(new EventArgs());
                        if (_args != null)
                        {
                            OnAfterControlValidation(_args);

                            if (!_args.Valid)
                            {
                                SetControlFocus((Control)_control);
                                bool _valid = IsValid((Control)_control, _args.Valid, _args.Notification);
                                break;
                            }
                        }
                    }
                }

                if (_args.Valid)
                {
                    _args = null; Materia.RefreshAndManageCurrentProcess();
                    _args = Validate(Binding, _args);
                }
            }

            if (_args == null) _args = new DataBinderValidationEventArgs();
            OnAfterValidation(_args);

            return _args;
        }