private void SwagColumnHeader_MoveClick(object sender, RoutedEventArgs e) { MenuItem menuItem = (MenuItem)sender; SwagDataColumn swagDataColumn = (SwagDataColumn)menuItem.DataContext; SwagLogger.LogStart(this, "Move Column |col={Column}|", swagDataColumn.ColumnName); Int32 offSet = Int32.Parse(menuItem.Tag.ToString()); Int32 targetSequence = swagDataColumn.ColSeq + offSet; if (targetSequence < 0) { targetSequence = 0; } else if (targetSequence > swagDataColumn.SwagDataTable.Columns.Count - 1) { targetSequence = swagDataColumn.SwagDataTable.Columns.Count - 1; } swagDataColumn.SetSequence(targetSequence); swagDataColumn.SwagDataTable.ResetColumnsCommand.Execute(null); SwagLogger.LogEnd(this, "Move Column |col={Column}|", swagDataColumn.ColumnName); }
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); }