Exemplo n.º 1
0
        private void butOK_Click(object sender, EventArgs e)
        {
            if (_gridCurIndex > 0)
            {
                _listTableHeaders[_gridCurIndex].PickList = string.Join("\r\n", _listComboOptions);
            }
            //Set primary key to correct name-----------------------------------------------------------------------
            gridMain.Rows[0].Cells[0].Text = _wikiListCurName + "Num"; //prevents exceptions from occuring when user tries to rename PK.
            //Validate column names---------------------------------------------------------------------------------
            for (int i = 0; i < gridMain.Rows.Count; i++)              //ODGridCell colNameCell in gridMain.Rows[0].Cells){
            {
                if (Regex.IsMatch(gridMain.Rows[i].Cells[0].Text, @"^\d"))
                {
                    MsgBox.Show(this, "Column cannot start with numbers.");
                    return;
                }
                if (Regex.IsMatch(gridMain.Rows[i].Cells[0].Text, @"\s"))
                {
                    MsgBox.Show(this, "Column names cannot contain spaces.");
                    return;
                }
                if (Regex.IsMatch(gridMain.Rows[i].Cells[0].Text, @"\W"))                //W=non-word chars
                {
                    MsgBox.Show(this, "Column names cannot contain special characters.");
                    return;
                }
            }
            //Check for reserved words--------------------------------------------------------------------------------
            for (int i = 0; i < gridMain.Rows.Count; i++)         //ODGridCell colNameCell in gridMain.Rows[0].Cells){
            {
                if (DbHelper.isMySQLReservedWord(gridMain.Rows[i].Cells[0].Text))
                {
                    MessageBox.Show(Lan.g(this, "Column name is a reserved word in MySQL") + ":" + gridMain.Rows[i].Cells[0].Text);
                    return;
                }
                //primary key is caught by duplicate column name logic.
            }
            //Check for duplicates-----------------------------------------------------------------------------------
            List <string> listColNamesCheck = new List <string>();

            for (int i = 0; i < gridMain.Rows.Count; i++)         //ODGridCell colNameCell in gridMain.Rows[0].Cells){
            {
                if (listColNamesCheck.Contains(gridMain.Rows[i].Cells[0].Text))
                {
                    MessageBox.Show(Lan.g(this, "Duplicate column name detected") + ":" + gridMain.Rows[0].Cells[i].Text);
                    return;
                }
                listColNamesCheck.Add(gridMain.Rows[i].Cells[0].Text);
            }
            //Validate column widths---------------------------------------------------------------------------------
            for (int i = 0; i < gridMain.Rows.Count; i++)                 //ODGridCell colNameCell in gridMain.Rows[0].Cells){
            {
                if (Regex.IsMatch(gridMain.Rows[i].Cells[1].Text, @"\D")) // "\D" matches any non-decimal character
                {
                    MsgBox.Show(this, "Column widths must only contain positive integers.");
                    return;
                }
                if (gridMain.Rows[i].Cells[1].Text.Contains("-") ||
                    gridMain.Rows[i].Cells[1].Text.Contains(".") ||
                    gridMain.Rows[i].Cells[1].Text.Contains(","))                        //inlcude the comma for international support. For instance Pi = 3.1415 or 3,1415 depending on your region
                {
                    MsgBox.Show(this, "Column widths must only contain positive integers.");
                    return;
                }
            }
            //save values to List<WikiListHeaderWidth> TableHeaders
            for (int i = 0; i < _listTableHeaders.Count; i++)
            {
                _listTableHeaders[i].ColName  = PIn.String(gridMain.Rows[i].Cells[0].Text);
                _listTableHeaders[i].ColWidth = PIn.Int(gridMain.Rows[i].Cells[1].Text);
            }
            //Save data to database-----------------------------------------------------------------------------------
            try {
                WikiListHeaderWidths.UpdateNamesAndWidths(_wikiListCurName, _listTableHeaders);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);                //will throw exception if table schema has changed since the window was opened.
                DialogResult = DialogResult.Cancel;
            }
            DataValid.SetInvalid(InvalidType.Wiki);
            DialogResult = DialogResult.OK;
        }
Exemplo n.º 2
0
 private void butOK_Click(object sender, EventArgs e)
 {
     if (_gridCurCell.Y > 0 && _gridCurCell.Y < _listTableHeaders.Count)         //no pick list option for the PK col
     {
         _listTableHeaders[_gridCurCell.Y].PickList = string.Join("\r\n", _listStringPick);
     }
     #region Validation
     List <string> listColNames = new List <string>();
     for (int i = 1; i < gridMain.ListGridRows.Count; i++)       //start with index 1, first col is PK
     {
         string colName  = gridMain.ListGridRows[i].Cells[0].Text;
         string colWidth = gridMain.ListGridRows[i].Cells[1].Text;
         #region Validate Column Widths
         if (Regex.IsMatch(colWidth, @"\D"))                // "\D" matches any non-decimal character
         {
             MsgBox.Show(this, "Column widths must only contain positive integers.");
             return;
         }
         //inlcude the comma for international support. For instance Pi = 3.1415 or 3,1415 depending on your region
         if (new[] { '-', '.', ',' }.Any(x => colWidth.Contains(x)))
         {
             MsgBox.Show(this, "Column widths must only contain positive integers.");
             return;
         }
         #endregion Validate Column Widths
         #region Validate Column Names
         if (listColNames.Contains(colName))
         {
             MessageBox.Show(Lan.g(this, $"Duplicate column name detected") + ": " + colName);
             return;
         }
         listColNames.Add(colName);
         if (i == 0)               //don't check PK column name, since it can't be changed
         {
             continue;
         }
         if (Regex.IsMatch(colName, @"^\d"))
         {
             MsgBox.Show(this, "Column cannot start with numbers.");
             return;
         }
         if (Regex.IsMatch(colName, @"\s"))
         {
             MsgBox.Show(this, "Column names cannot contain spaces.");
             return;
         }
         if (Regex.IsMatch(colName, @"\W"))                //W=non-word chars
         {
             MsgBox.Show(this, "Column names cannot contain special characters.");
             return;
         }
         //Check for reserved words--------------------------------------------------------------------------------
         if (DbHelper.isMySQLReservedWord(colName))
         {
             MessageBox.Show(Lan.g(this, "Column name is a reserved word in MySQL") + ": " + colName);
             return;
         }
         #endregion Validate Column Names
     }
     #endregion Validation
     #region Update _listTableHeaders
     for (int i = 0; i < _listTableHeaders.Count; i++)
     {
         if (i > 0)               //don't allow renaming the first column, it's the PK
         {
             _listTableHeaders[i].ColName = PIn.String(gridMain.ListGridRows[i].Cells[0].Text);
         }
         _listTableHeaders[i].ColWidth = PIn.Int(gridMain.ListGridRows[i].Cells[1].Text);
     }
     #endregion Update _listTableHeaders
     #region Try Update DB
     try {
         WikiListHeaderWidths.UpdateNamesAndWidths(_wikiListCurName, _listTableHeaders);
     }
     catch (Exception ex) {
         MessageBox.Show(ex.Message);                //will throw exception if table schema has changed since the window was opened.
         DialogResult = DialogResult.Cancel;
     }
     #endregion Try Update DB
     DataValid.SetInvalid(InvalidType.Wiki);
     DialogResult = DialogResult.OK;
 }