/// <summary> /// Returns ROWS from User Defined Custom Table. Requires TableInfo with information about columns and it's type of data. /// Obtainable with GetInfoAboutTables. /// </summary> /// <returns></returns> public static CustomTableRows GetAllRowsFromCustomTable(TableInfo customTableInfo) { List <CellContent> collection; CustomTableRows customTableRow = new CustomTableRows(); //TableInfo userDefinedTableContent = null; TABLE CONTENTS ? string command = @"SELECT * FROM " + customTableInfo.TableName; var query = new MySqlCommand(command, dbAgent.GetConnection()); dbAgent.GetConnection().Open(); try { var reader = query.ExecuteReader(); while (reader.Read()) { collection = new List <CellContent>(); // for every columns in row for (int i = 0; i < customTableInfo.Count; i++) { // To determine what action we need to do, we need to check for every column type. switch (customTableInfo.ColumnInfos_Row[i].ColumnType) { case ColumnType.DataType: var DateTime = (DateTime)reader[customTableInfo.ColumnInfos_Row[i].Name]; collection.Add(new CellContent(DateTime, (int)reader["ID"])); break; case ColumnType.Description: var descr = reader[customTableInfo.ColumnInfos_Row[i].Name].ToString(); collection.Add(new CellContent(descr, (int)reader["ID"])); break; case ColumnType.ShortText: var shrtText = reader[customTableInfo.ColumnInfos_Row[i].Name].ToString(); collection.Add(new CellContent(shrtText, (int)reader["ID"])); break; case ColumnType.Numeric: var num = (int)reader[customTableInfo.ColumnInfos_Row[i].Name]; collection.Add(new CellContent(num, (int)reader["ID"])); break; default: throw new Exception("Custom Table Helper GetAllRowsFromCustomTable method, switch Error "); } } // ROWS customTableRow.AddRow(collection); } } catch (MySqlException ex) { MessageBox.Show("Nastąpił błąd połączenia z bazą danych. Jeśli problem będzie się powtrzał skontaktuj się z zarządcą bazy danych", "Błąd połączenia z bazą danych" + Environment.NewLine + ex.ErrorCode, MessageBoxButtons.OK, MessageBoxIcon.Error); Debug.WriteLine("Exception Message: " + ex.Message); Debug.WriteLine("Exception Error Code: " + ex.ErrorCode); Debug.WriteLine("Exception Source: " + ex.Source); } catch (Exception ex) { MessageBox.Show("Wystąpił błąd " + Environment.NewLine + ex.Message + "Operacja nie powiodła się", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); Debug.WriteLine("Exception Message: " + ex.Message); Debug.WriteLine("Exception Source: " + ex.Source); Debug.WriteLine(" Custom Table Helper GetAllRowsFromCustomDb"); } finally { dbTools.dbAgent.GetConnection().Close(); } return(customTableRow); }
private void InitDataGrid() { if (userDefinedTable == null) { MessageBox.Show("Obecnie nie ma żadnych " + dbHelpers.NamesTypes.CommonCustomTableName_POLISH_ADJECTIVE, "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.Dispose(); return; } //DODAWANIA CUSTOM TABLE dgCustomTable. dgCustomTable.Rows.Clear(); dgCustomTable.Columns.Clear(); dgCustomTable.Columns.Add("OrdinalNumber", "L.P."); foreach (ColumnInfo columnInfo in userDefinedTable.ColumnInfos_Row) { dgCustomTable.Columns.Add(columnInfo.Name, columnInfo.Name); if (columnInfo.ColumnType == ColumnType.DataType) { dgCustomTable.Columns[columnInfo.Name].ValueType = typeof(DateTime); } } //////////// dgCustomTable.Columns.Add("TableID", "TableID"); dgCustomTable.Columns["TableID"].Visible = false; dgCustomTable.SelectionMode = DataGridViewSelectionMode.FullRowSelect; /////////// int customRowCount = CustomTableHelper.GetRowCount(userDefinedTable.TableName); tableRows = CustomTableHelper.GetAllRowsFromCustomTable(userDefinedTable); for (int i = 0; i < customRowCount; i++) { // Columns var currentRow = tableRows.Row[i]; dgCustomTable.Rows.Add(1); dgCustomTable.Rows[i].Cells[0].Value = (int)i + 1; // Columns of row for (int j = 0; j < currentRow.Count; j++) { switch (currentRow[j].GetType()) { case TypeCode.DateTime: // buf_DataTime = (DateTime) currentRow[i].Value; dgCustomTable.Rows[i].Cells[j + 1].ValueType = typeof(DateTime); dgCustomTable.Rows[i].Cells[j + 1].Value = (DateTime)currentRow[j].Value; break; case TypeCode.String: // buf_descr = (string) currentRow[i].Value; dgCustomTable.Rows[i].Cells[j + 1].Value = (string)currentRow[j].Value; break; case TypeCode.Char: // buf_shortText = (string) currentRow[i].Value; dgCustomTable.Rows[i].Cells[j + 1].Value = (string)currentRow[j].Value; break; case TypeCode.Int32: // buf_Numeric = (int) currentRow[i].Value; dgCustomTable.Rows[i].Cells[j + 1].ValueType = typeof(int); dgCustomTable.Rows[i].Cells[j + 1].Value = (int)currentRow[j].Value; break; default: throw new Exception("CustomTableFrame Type Code Error"); } } //////// dgCustomTable.Rows[i].Cells[dgCustomTable.Rows[i].Cells.Count - 1].Value = currentRow[currentRow.Count - 1].ID; //////// } }