private void SwagColumnHeader_RenameClick(object sender, RoutedEventArgs e) { Button btnRename = (Button)sender; MenuItem miParent = (MenuItem)((MenuItem)((Grid)btnRename.Parent).Parent).Parent; SwagDataColumn swagDataColumn = (SwagDataColumn)btnRename.DataContext; ContextMenu contextMenu = DependencyObjectHelper.TryFindParent <ContextMenu>(btnRename); Grid grid = miParent.FindLogicalChild <Grid>("gridRename"); String originalName = swagDataColumn.ColumnName; String newColName = grid.FindVisualChild <TextBox>().Text; SwagLogger.LogStart(this, "Column Rename |orig={Column}|new={NewColName}|", originalName, newColName); swagDataColumn.Rename(newColName); contextMenu.IsOpen = false; SwagLogger.LogEnd(this, "Column Rename |orig={Column}|new={NewColName}|", originalName, newColName); }
private async void SwagColumnHeader_ConvertClick(object sender, RoutedEventArgs e) { MenuItem menuItem = (MenuItem)sender; MenuItem miParent = (MenuItem)menuItem.Parent; SwagDataColumn originalSwagDataColumn = (SwagDataColumn)menuItem.DataContext; SwagDataTable swagDataTable = originalSwagDataColumn.SwagDataTable; Type targetType = (Type)menuItem.Tag; Grid grid = miParent.FindLogicalChild <Grid>("gridConvertOptions"); Boolean keepOriginal = grid.FindVisualChild <CheckBox>().IsChecked ?? false; String defaultValueText = grid.FindVisualChild <TextBox>().Text; DataTable dt = originalSwagDataColumn.SwagDataTable.DataTable; String newColName = $"{originalSwagDataColumn.ColumnName}{targetType.Name}"; swagDataTable.DelaySave = true; SwagLogger.LogStart(this, "Convert Column |col={Column}|", originalSwagDataColumn.ColumnName); Int32 count = 1; while (dt.Columns.Contains(newColName)) { newColName = $"{originalSwagDataColumn.ColumnName}{count}{targetType.Name}"; count++; } SwagDataColumn newSwagDataColumn = new SwagDataColumn() { ColumnName = newColName, DataType = targetType }; swagDataTable.Columns.Add(newSwagDataColumn.ColumnName, newSwagDataColumn); SwagWindow.GlobalIsBusy = true; await Task.Run(() => { Func <Type, String, object, object> convert = (type, input, defaultOutput) => { switch (type.Name) { case "Int32": if (Int32.TryParse(input, out Int32 outInt32)) { return(outInt32); } break; case "Decimal": if (Decimal.TryParse(input, out Decimal outDecimal)) { return(outDecimal); } break; case "DateTime": if (DateTime.TryParse(input, out DateTime outDateTime)) { return(outDateTime); } break; case "TimeSpan": if (TimeSpan.TryParse(input, out TimeSpan outTimeSpan)) { return(outTimeSpan); } break; case "String": default: return(input.ToString()); } return(defaultOutput); }; #region Resolve defaultValue Object defaultValue = DBNull.Value; if (targetType.GetTypeCode() != TypeCode.String) { defaultValue = convert(targetType, defaultValueText, DBNull.Value); } else { defaultValue = defaultValueText; } #endregion Resolve defaultValue #region Resolve Rows swagDataTable.DelaySave = true; using (var scope = swagDataTable.GetFreezeListScope()) { foreach (DataRow dr in dt.Rows) { dr[newColName] = convert(targetType, dr[$"{originalSwagDataColumn.ColumnName}"].ToString(), defaultValue); } } swagDataTable.DelaySave = false; #endregion Resolve Rows }); SwagWindow.GlobalIsBusy = false; if (!keepOriginal) { newSwagDataColumn.SetSequence(originalSwagDataColumn.ColSeq); originalSwagDataColumn.Remove(); newSwagDataColumn.Rename(originalSwagDataColumn.ColumnName); } SwagLogger.LogEnd(this, "Convert Column |col={Column}|", originalSwagDataColumn.ColumnName); }