/// <summary>
        /// Creates a new <see cref="Epi.Fields.CheckboxColumn"/> in <see cref="Epi.Fields.GridField"/>.
        /// </summary>
        /// <param name="column"><see cref="Epi.Fields.CheckboxColumn"/> to create.</param>
        /// <returns>Id of the newly created column.</returns>
        public int CreateGridColumn(CheckboxColumn column)
        {
            try
            {
                #region InputValidation
                if (column == null)
                {
                    throw new ArgumentNullException("CheckboxColumn");
                }
                #endregion

                Query insertQuery = db.CreateQuery("insert into metaGridColumns([Name], [Width], [Position], [FieldTypeId], [Text], [ShouldRepeatLast], [IsRequired], [IsReadOnly], [FieldId]) " +
                    "values (@Name, @Width, @Position, @FieldTypeId, @Text, @ShouldRepeatLast, @IsRequired, @IsReadOnly, @FieldId)");

                insertQuery.Parameters.Add(new QueryParameter("@Name", DbType.String, column.Name));
                insertQuery.Parameters.Add(new QueryParameter("@Width", DbType.Int32, column.Width));
                insertQuery.Parameters.Add(new QueryParameter("@Position", DbType.Int32, column.Position));
                insertQuery.Parameters.Add(new QueryParameter("@FieldTypeId", DbType.Int32, (int)column.GridColumnType));
                insertQuery.Parameters.Add(new QueryParameter("@Text", DbType.String, column.Text));
                insertQuery.Parameters.Add(new QueryParameter("@ShouldRepeatLast", DbType.Boolean, column.ShouldRepeatLast));
                insertQuery.Parameters.Add(new QueryParameter("@IsRequired", DbType.Boolean, column.IsRequired));
                insertQuery.Parameters.Add(new QueryParameter("@IsReadOnly", DbType.Boolean, column.IsReadOnly));
                insertQuery.Parameters.Add(new QueryParameter("@FieldId", DbType.Int32, column.Grid.Id));

                db.ExecuteNonQuery(insertQuery);
                return GetMaxGridColumnId(column.Grid.Id);
            }
            catch (Exception ex)
            {
                throw new GeneralException("Could not create grid Text column in the database.", ex);
            }
            finally
            {

            }
        }
 /// <summary>
 /// Save column data 
 /// </summary>
 private void SaveColumnData()
 {
     switch ((MetaFieldType)cmbFieldType.SelectedValue)
     {
         case MetaFieldType.Text:
             TextColumn textColumn = new TextColumn(grid);
             textColumn.Name = txtFieldName.Text;
             textColumn.Text = txtColumnName.Text;
             textColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
             textColumn.IsRequired = cbxRequired.Checked;
             textColumn.IsReadOnly = cbxReadOnly.Checked;
             textColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;//gridColumnsTable.Columns.Count;
             textColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth;
             if (!string.IsNullOrEmpty(cmbSize.Text))
             {
                 textColumn.Size = int.Parse(cmbSize.Text);
             }
             textColumn.Grid = grid;
             gridColumns.Add(textColumn);
             SetColumnStyle(textColumn);
             break;
         case MetaFieldType.Number:
             NumberColumn numberColumn = new NumberColumn(grid);
             numberColumn.Name = txtFieldName.Text;
             numberColumn.Text = txtColumnName.Text;
             numberColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
             numberColumn.IsRequired = cbxRequired.Checked;
             numberColumn.IsReadOnly = cbxReadOnly.Checked;
             numberColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;
             numberColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth;
             numberColumn.Pattern = cmbPattern.Text;
             if (cbxRange.Checked)
             {
                 numberColumn.Lower = txtLower.Text;
                 numberColumn.Upper = txtUpper.Text;
             }
             gridColumns.Add(numberColumn);
             SetColumnStyle(numberColumn);
             break;
         case MetaFieldType.PhoneNumber:
             PhoneNumberColumn phoneNumberColumn = new PhoneNumberColumn(grid);
             phoneNumberColumn.Name = txtFieldName.Text;
             phoneNumberColumn.Text = txtColumnName.Text;
             phoneNumberColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
             phoneNumberColumn.IsRequired = cbxRequired.Checked;
             phoneNumberColumn.IsReadOnly = cbxReadOnly.Checked;
             phoneNumberColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;
             phoneNumberColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth;
             phoneNumberColumn.Pattern = cmbPattern.Text;
             gridColumns.Add(phoneNumberColumn);
             SetColumnStyle(phoneNumberColumn);
             break;
         case MetaFieldType.Date:
             SetContiguousColumnProperties(new DateColumn(grid));
             break;
         case MetaFieldType.Time:
             SetContiguousColumnProperties(new TimeColumn(grid));
             break;
         case MetaFieldType.DateTime:
             SetContiguousColumnProperties(new DateTimeColumn(grid));
             break;
         case MetaFieldType.CommentLegal:
             DDLColumnOfCommentLegal commentLegalColumn = new DDLColumnOfCommentLegal(grid);
             commentLegalColumn.ShouldSort = shouldSort;
             commentLegalColumn.SourceTableName = sourceTableName;
             commentLegalColumn.TextColumnName = textColumnName;
             commentLegalColumn.IsExclusiveTable = isExclusiveTable;
             commentLegalColumn.Name = txtFieldName.Text;
             commentLegalColumn.Text = txtColumnName.Text;
             commentLegalColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
             commentLegalColumn.IsRequired = cbxRequired.Checked;
             commentLegalColumn.IsReadOnly = cbxReadOnly.Checked;
             commentLegalColumn.IsUniqueField = cbxUniqueField.Checked;
             commentLegalColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;
             commentLegalColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth;
             commentLegalColumn.Grid = grid;
             gridColumns.Add(commentLegalColumn);
             SetColumnStyle(commentLegalColumn);
             break;
         case MetaFieldType.LegalValues:
             DDLColumnOfLegalValues legalValuesColumn = new DDLColumnOfLegalValues(grid);
             legalValuesColumn.ShouldSort = shouldSort;
             legalValuesColumn.SourceTableName = sourceTableName;
             legalValuesColumn.TextColumnName = textColumnName;
             legalValuesColumn.IsExclusiveTable = isExclusiveTable;
             legalValuesColumn.Name = txtFieldName.Text;
             legalValuesColumn.Text = txtColumnName.Text;
             legalValuesColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
             legalValuesColumn.IsRequired = cbxRequired.Checked;
             legalValuesColumn.IsReadOnly = cbxReadOnly.Checked;
             legalValuesColumn.IsUniqueField = cbxUniqueField.Checked;
             legalValuesColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;
             legalValuesColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth; ;
             legalValuesColumn.Grid = grid;
             gridColumns.Add(legalValuesColumn);
             SetColumnStyle(legalValuesColumn);
             break;
         case MetaFieldType.Checkbox:
             CheckboxColumn checkboxColumn = new CheckboxColumn(grid);
             checkboxColumn.Name = txtFieldName.Text;
             checkboxColumn.Text = txtColumnName.Text;
             checkboxColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
             checkboxColumn.IsRequired = cbxRequired.Checked;
             checkboxColumn.IsReadOnly = cbxReadOnly.Checked;
             checkboxColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;//gridColumnsTable.Columns.Count;
             checkboxColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth;
             checkboxColumn.Grid = grid;
             gridColumns.Add(checkboxColumn);
             SetColumnStyle(checkboxColumn);
             break;
         case MetaFieldType.YesNo:
             YesNoColumn yesNoColumn = new YesNoColumn(grid);
             yesNoColumn.ShouldSort = shouldSort;
             yesNoColumn.SourceTableName = sourceTableName;
             yesNoColumn.TextColumnName = textColumnName;
             yesNoColumn.IsExclusiveTable = isExclusiveTable;
             yesNoColumn.Name = txtFieldName.Text;
             yesNoColumn.Text = txtColumnName.Text;
             yesNoColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
             yesNoColumn.IsRequired = cbxRequired.Checked;
             yesNoColumn.IsReadOnly = cbxReadOnly.Checked;
             yesNoColumn.IsUniqueField = cbxUniqueField.Checked;
             yesNoColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;
             yesNoColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth; ;
             yesNoColumn.Grid = grid;
             gridColumns.Add(yesNoColumn);
             SetColumnStyle(yesNoColumn);
             break;
     }
 }
        /// <summary>
        /// Updates a CheckboxColumn saved in the database.
        /// </summary>
        /// <param name="column">Column to be updated</param>
        public void UpdateGridColumn(CheckboxColumn column)
        {
            try
            {
                Query updateQuery = db.CreateQuery("update [metaGridColumns] set [Name] = @Name, [Width] = @Width, [Position] = @Position, [FieldTypeId] = @FieldTypeId, [Text] = @Text, [ShouldRepeatLast] = @ShouldRepeatLast, [IsRequired] = @IsRequired, [IsReadOnly] = @IsReadOnly " +
                    "where [GridColumnId] = @GridColumnId");

                updateQuery.Parameters.Add(new QueryParameter("@Name", DbType.String, column.Name));
                updateQuery.Parameters.Add(new QueryParameter("@Width", DbType.Int32, column.Width));
                updateQuery.Parameters.Add(new QueryParameter("@Position", DbType.Int32, column.Position));
                updateQuery.Parameters.Add(new QueryParameter("@FieldTypeId", DbType.Int32, (int)column.GridColumnType));
                updateQuery.Parameters.Add(new QueryParameter("@Text", DbType.String, column.Text));
                updateQuery.Parameters.Add(new QueryParameter("@ShouldRepeatLast", DbType.Boolean, column.ShouldRepeatLast));
                updateQuery.Parameters.Add(new QueryParameter("@IsRequired", DbType.Boolean, column.IsRequired));
                updateQuery.Parameters.Add(new QueryParameter("@IsReadOnly", DbType.Boolean, column.IsReadOnly));
                updateQuery.Parameters.Add(new QueryParameter("@GridColumnId", DbType.Int32, column.Id));

                db.ExecuteNonQuery(updateQuery);
            }
            catch (Exception ex)
            {
                throw new GeneralException("Could not update grid column in the database", ex);
            }
        }
        /// <summary>
        /// Updates column data 
        /// </summary>
        private void UpdateColumnData()
        {
            currentGridColumn = hti.Column + iColumnOffset;
            MetaFieldType metaFieldTypeEnum = (MetaFieldType)cmbFieldType.SelectedValue;

            switch (metaFieldTypeEnum)
            {
                case MetaFieldType.Text:
                    TextColumn textColumn;
                    textColumn = new TextColumn(grid);
                    textColumn.Name = txtFieldName.Text;
                    textColumn.Text = txtColumnName.Text;
                    textColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
                    textColumn.IsRequired = cbxRequired.Checked;
                    textColumn.IsReadOnly = cbxReadOnly.Checked;
                    textColumn.Position = hti.Column;
                    textColumn.Width = this.dgColumns.TableStyles["GridColumns"].GridColumnStyles[hti.Column].Width;

                    if (!string.IsNullOrEmpty(cmbSize.Text))
                    {
                        textColumn.Size = int.Parse(cmbSize.Text);
                    }
                    textColumn.Grid = grid;
                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, textColumn);
                    UpdateColumnStyle(currentGridColumn, textColumn);
                    break;

                case MetaFieldType.Number:
                    NumberColumn numberColumn;
                    numberColumn = new NumberColumn(grid);
                    numberColumn.Name = txtFieldName.Text;
                    numberColumn.Text = txtColumnName.Text;
                    numberColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
                    numberColumn.IsRequired = cbxRequired.Checked;
                    numberColumn.IsReadOnly = cbxReadOnly.Checked;
                    numberColumn.Position = hti.Column;
                    numberColumn.Width = this.dgColumns.TableStyles["GridColumns"].GridColumnStyles[hti.Column].Width; // 500;
                    numberColumn.Pattern = cmbPattern.Text;
                    if (cbxRange.Checked)
                    {
                        numberColumn.Lower = txtLower.Text;
                        numberColumn.Upper = txtUpper.Text;
                    }

                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, numberColumn);
                    UpdateColumnStyle(currentGridColumn, numberColumn);
                    break;

                case MetaFieldType.PhoneNumber:
                    PhoneNumberColumn phoneNumberColumn;
                    phoneNumberColumn = new PhoneNumberColumn(grid);
                    phoneNumberColumn.Name = txtFieldName.Text;
                    phoneNumberColumn.Text = txtColumnName.Text;
                    phoneNumberColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
                    phoneNumberColumn.IsRequired = cbxRequired.Checked;
                    phoneNumberColumn.IsReadOnly = cbxReadOnly.Checked;
                    phoneNumberColumn.Position = hti.Column;
                    phoneNumberColumn.Width = this.dgColumns.TableStyles["GridColumns"].GridColumnStyles[hti.Column].Width;
                    phoneNumberColumn.Pattern = cmbPattern.Text;
                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, phoneNumberColumn);
                    UpdateColumnStyle(currentGridColumn, phoneNumberColumn);
                    break;

                case MetaFieldType.Date:
                case MetaFieldType.Time:
                case MetaFieldType.DateTime:

                    ContiguousColumn column;
                    switch (metaFieldTypeEnum)
                    {
                        case MetaFieldType.Date:
                            column = new DateColumn(grid);
                            break;
                        case MetaFieldType.Time:
                            column = new TimeColumn(grid);
                            break;
                        case MetaFieldType.DateTime:
                            column = new DateTimeColumn(grid);
                            break;
                        default:
                            column = new DateColumn(grid);
                            break;
                    }

                    column.Name = txtFieldName.Text;
                    column.Text = txtColumnName.Text;
                    column.ShouldRepeatLast = cbxRepeatLast.Checked;
                    column.IsRequired = cbxRequired.Checked;
                    column.IsReadOnly = cbxReadOnly.Checked;
                    column.Position = hti.Column;
                    column.Width = this.dgColumns.TableStyles["GridColumns"].GridColumnStyles[hti.Column].Width; // 500;
                    column.Pattern = cmbPattern.Text;
                    if (cbxRange.Checked)
                    {
                        column.Lower = txtLower.Text;
                        column.Upper = txtUpper.Text;
                    }
                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, column);
                    UpdateColumnStyle(currentGridColumn, column);
                    break;

                case MetaFieldType.CommentLegal:
                    DDLColumnOfCommentLegal commentLegalColumn;
                    commentLegalColumn = new DDLColumnOfCommentLegal(grid);

                    commentLegalColumn.Name = txtFieldName.Text;
                    commentLegalColumn.Text = txtColumnName.Text;
                    commentLegalColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
                    commentLegalColumn.IsRequired = cbxRequired.Checked;
                    commentLegalColumn.IsReadOnly = cbxReadOnly.Checked;
                    commentLegalColumn.IsUniqueField = cbxUniqueField.Checked;
                    commentLegalColumn.Position = hti.Column;
                    commentLegalColumn.Width = this.dgColumns.TableStyles["GridColumns"].GridColumnStyles[hti.Column].Width;
                    if (txtDataSource.Text.Length > 4)
                    {
                        commentLegalColumn.SourceTableName = txtDataSource.Text.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
                        commentLegalColumn.TextColumnName = txtDataSource.Text.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
                    }
                    commentLegalColumn.Grid = grid;
                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, commentLegalColumn);
                    UpdateColumnStyle(currentGridColumn, commentLegalColumn);
                    btnDataSource.Enabled = true;
                    cbxUniqueField.Enabled = true;
                    break;

                case MetaFieldType.LegalValues:
                    DDLColumnOfLegalValues legalValuesColumn;
                    legalValuesColumn = new DDLColumnOfLegalValues(grid);
                    legalValuesColumn.Name = txtFieldName.Text;
                    legalValuesColumn.Text = txtColumnName.Text;
                    legalValuesColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
                    legalValuesColumn.IsRequired = cbxRequired.Checked;
                    legalValuesColumn.IsReadOnly = cbxReadOnly.Checked;
                    legalValuesColumn.IsUniqueField = cbxUniqueField.Checked;
                    legalValuesColumn.Position = hti.Column;
                    legalValuesColumn.Width = this.dgColumns.TableStyles["GridColumns"].GridColumnStyles[hti.Column].Width;
                    if (txtDataSource.Text.Length > 4)
                    {
                        legalValuesColumn.SourceTableName = txtDataSource.Text.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
                        legalValuesColumn.TextColumnName = txtDataSource.Text.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
                    }
                    legalValuesColumn.Grid = grid;
                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, legalValuesColumn);
                    UpdateColumnStyle(currentGridColumn, legalValuesColumn);
                    btnDataSource.Enabled = true;
                    cbxUniqueField.Enabled = true;
                    break;

                case MetaFieldType.Checkbox:
                    CheckboxColumn checkboxColumn = new CheckboxColumn(grid);
                    checkboxColumn.Name = txtFieldName.Text;
                    checkboxColumn.Text = txtColumnName.Text;
                    checkboxColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
                    checkboxColumn.IsRequired = cbxRequired.Checked;
                    checkboxColumn.IsReadOnly = cbxReadOnly.Checked;
                    checkboxColumn.Position = gridColumnsTable.Columns[txtFieldName.Text].Ordinal;
                    checkboxColumn.Width = this.dgColumns.TableStyles["GridColumns"].PreferredColumnWidth;
                    checkboxColumn.Grid = grid;
                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, checkboxColumn);
                    UpdateColumnStyle(currentGridColumn, checkboxColumn);
                    btnDataSource.Enabled = true;
                    cbxUniqueField.Enabled = true;
                    break;

                case MetaFieldType.YesNo:
                    YesNoColumn yesNoColumn;
                    yesNoColumn = new YesNoColumn(grid);
                    yesNoColumn.Name = txtFieldName.Text;
                    yesNoColumn.Text = txtColumnName.Text;
                    yesNoColumn.ShouldRepeatLast = cbxRepeatLast.Checked;
                    yesNoColumn.IsRequired = cbxRequired.Checked;
                    yesNoColumn.IsReadOnly = cbxReadOnly.Checked;
                    yesNoColumn.IsUniqueField = cbxUniqueField.Checked;
                    yesNoColumn.Position = hti.Column;
                    yesNoColumn.Width = this.dgColumns.TableStyles["GridColumns"].GridColumnStyles[hti.Column].Width;

                    if (txtDataSource.Text.Length > 4)
                    {
                        yesNoColumn.SourceTableName = txtDataSource.Text.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
                        yesNoColumn.TextColumnName = txtDataSource.Text.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
                    }

                    yesNoColumn.Grid = grid;
                    gridColumns.RemoveAt(currentGridColumn);
                    gridColumns.Insert(currentGridColumn, yesNoColumn);
                    UpdateColumnStyle(currentGridColumn, yesNoColumn);
                    btnDataSource.Enabled = true;
                    cbxUniqueField.Enabled = true;
                    break;
            }
        }