private void SwagDataColumn_ViewClick(object sender, RoutedEventArgs e) { SwagDataColumn swagDataColumn = ((KeyValuePair <String, SwagDataColumn>)((MenuItem)sender).DataContext).Value; View(swagDataColumn); }
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); }
private void BindSourceGrid() { if (Source.DataTable != null && Source.DataTable.Columns.Count > 0) { #region DestID if (!Source.Columns.ContainsKey("DestID")) { SwagDataColumn sdc = new SwagDataColumn() { ColumnName = "DestID", DataType = typeof(string) //Saving a binding is currently not working //Binding = new Binding("[DestID]") { TargetNullValue = "----------", FallbackValue = "----------" } }; Source.Columns.Add("DestID", sdc); } #endregion DestID #region Mapping if (!Source.DataTable.Columns.Contains("Mapping")) { String mappingTemplate = @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:TestWPF;assembly=TestWPF' xmlns:swag='clr-namespace:SwagOverFlow.WPF.Controls;assembly=SwagOverFlow.WPF'> <swag:SwagComboBox DisplayMemberProperty ='{{descriptionField}}' ItemsSource='{Binding Dest.DataTable.DefaultView, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}' ValueMemberProperty='ItemID' Value='{Binding DestID}' Text='{Binding Mapping}'/> </DataTemplate>"; mappingTemplate = mappingTemplate.Replace("{{descriptionField}}", descriptionField); SwagDataColumn sdc = new SwagDataColumn() { ColumnName = "Mapping", DataType = typeof(string), DataTemplate = mappingTemplate }; Source.Columns.Add("Mapping", sdc); } #endregion Mapping #region (Source, Dest) //Need to load in proper order because of dependence on other Columns //if (!Source.Columns.ContainsKey("Source, Dest")) //{ // SwagDataColumnWPF sdc = new SwagDataColumnWPF() // { // ColumnName = "Source, Dest", // DataType = typeof(string), // Expression = "'(' + ItemID + ', ' + DestID + ')'" // }; // Source.Columns.Add("Source, Dest", sdc); //} #endregion (Source, Dest) //FIX_THIS //Source.Save(); } }