public static void RemoveSorting(object sender, DBTableControl.DBEditorTableControl mainTable) { DataGridColumn col = (((sender as MenuItem).Parent as ContextMenu).PlacementTarget as DataGridColumnHeader).Column; col.SortDirection = null; mainTable.Refresh(); }
public static void ClearAllHiddenMenuItem(DBTableControl.DBEditorTableControl mainTable) { //Prompt for confirmation. string text = "Are you sure you want to clear all saved hidden column information?"; string caption = "Clear Confirmation"; MessageBoxButton button = MessageBoxButton.YesNo; MessageBoxImage image = MessageBoxImage.Question; MessageBoxResult result = MessageBox.Show(text, caption, button, image); if (result == MessageBoxResult.Yes) { // Clear internal list. mainTable._hiddenColumns.Clear(); // Clear saved list. mainTable._savedconfig.HiddenColumns.Clear(); } mainTable.UpdateConfig(); if (mainTable.ShowAllColumns) { mainTable.Refresh(); } }
public static void EditVisibleListMenuItem(object sender, DBTableControl.DBEditorTableControl mainTable) { var hiddencolumnslisteditor = ColumnVisiblityHelper.Show(mainTable); System.Windows.Forms.DialogResult result = hiddencolumnslisteditor.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { mainTable._hiddenColumns.Clear(); mainTable._hiddenColumns = hiddencolumnslisteditor.RightList; foreach (DataColumn column in mainTable.CurrentTable.Columns) { if (hiddencolumnslisteditor.LeftList.Contains(column.ColumnName)) { column.ExtendedProperties["Hidden"] = false; mainTable.dbDataGrid.Columns[column.Ordinal].Visibility = System.Windows.Visibility.Visible; } else { column.ExtendedProperties["Hidden"] = true; if (mainTable.ShowAllColumns) { mainTable.dbDataGrid.Columns[column.Ordinal].Visibility = System.Windows.Visibility.Visible; } else { mainTable.dbDataGrid.Columns[column.Ordinal].Visibility = System.Windows.Visibility.Hidden; } } } mainTable.UpdateConfig(); if (mainTable.ShowAllColumns) { mainTable.Refresh(); } } }
public static void ClearTableHiddenMenuItem(DBTableControl.DBEditorTableControl mainTable) { foreach (DataGridColumn column in mainTable.dbDataGrid.Columns) { DataColumn datacolumn = mainTable.CurrentTable.Columns[(string)column.Header]; if (!datacolumn.ExtendedProperties.ContainsKey("Hidden")) { datacolumn.ExtendedProperties.Add("Hidden", false); } datacolumn.ExtendedProperties["Hidden"] = false; column.Visibility = Visibility.Visible; } // Clear the internal hidden columns list. mainTable._hiddenColumns.Clear(); mainTable.UpdateConfig(); if (mainTable.ShowAllColumns) { mainTable.Refresh(); } }
public static void OnExecutedPaste(DBTableControl.DBEditorTableControl mainTable) { string clipboarddata = GetClipboardText(); int rowindex; int datarowindex; int columnindex; int datacolumnindex; if (ClipboardIsEmpty()) { // Clipboard Empty } else if (ClipboardContainsSingleCell()) { // Single Cell Paste string pastevalue = clipboarddata.Trim(); foreach (DataGridCellInfo cellinfo in mainTable.dbDataGrid.SelectedCells) { // Get both visible and data row index rowindex = mainTable.dbDataGrid.Items.IndexOf(cellinfo.Item); datarowindex = mainTable.CurrentTable.Rows.IndexOf((cellinfo.Item as DataRowView).Row); // Additional error checking for the visibleRows internal list. if (datarowindex >= mainTable._visibleRows.Count) { mainTable.UpdateVisibleRows(); } // Selecting cells while the table is filtered will select collapsed rows, so skip them here. if (mainTable._visibleRows[datarowindex] == System.Windows.Visibility.Collapsed) { continue; } // Get both visible and data column index columnindex = mainTable.dbDataGrid.Columns.IndexOf(cellinfo.Column); datacolumnindex = mainTable.CurrentTable.Columns.IndexOf((string)cellinfo.Column.Header); mainTable.CurrentTable.Rows[datarowindex].BeginEdit(); if (!TryPasteValue(mainTable.CurrentTable, datarowindex, datacolumnindex, pastevalue)) { // Paste Error } mainTable.CurrentTable.Rows[datarowindex].EndEdit(); mainTable.RefreshCell(rowindex, columnindex); } } else if (!ClipboardContainsOnlyRows(mainTable)) { if (mainTable.dbDataGrid.SelectedItems.OfType <DataRowView>().Count() != mainTable.dbDataGrid.SelectedItems.Count) { // The blank row is selected, abort. MessageBox.Show("Only select the blank row when pasting rows.", "Selection Error", MessageBoxButton.OK, MessageBoxImage.Warning); return; } // Use -1 values to indicate that nothing is selected, and to paste any full rows the // clipboard might contain as new rows. int basecolumnindex = -1; rowindex = -1; if (mainTable.dbDataGrid.SelectedCells.Count > 1) { // User has more than 1 cells selected, therefore the selection must match the clipboard data. if (!PasteMatchesSelection(mainTable, clipboarddata)) { // Warn user if (MessageBox.Show("Warning! Cell selection does not match copied data, attempt to paste anyway?", "Selection Error", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) { return; } } // Set values to the first cell's coordinates // Get both visible and data row index rowindex = mainTable.dbDataGrid.Items.IndexOf(mainTable.dbDataGrid.SelectedCells[0].Item); basecolumnindex = mainTable.dbDataGrid.SelectedCells[0].Column.DisplayIndex; // Determine upper left corner of selection foreach (DataGridCellInfo cellinfo in mainTable.dbDataGrid.SelectedCells) { rowindex = Math.Min(rowindex, mainTable.dbDataGrid.Items.IndexOf(cellinfo.Item)); basecolumnindex = Math.Min(basecolumnindex, cellinfo.Column.DisplayIndex); } } else if (mainTable.dbDataGrid.SelectedCells.Count == 1) { // User has 1 cell selected, assume it is the top left corner and attempt to paste. rowindex = mainTable.dbDataGrid.Items.IndexOf(mainTable.dbDataGrid.SelectedCells[0].Item); basecolumnindex = mainTable.dbDataGrid.SelectedCells[0].Column.DisplayIndex; } List <string> pasteinstructions = new List <string>(); foreach (string line in clipboarddata.Split('\n')) { columnindex = basecolumnindex; if (rowindex > mainTable.CurrentTable.Rows.Count - 1 || columnindex == -1) { if (IsLineARow(mainTable, line)) { // We have a full row, but no where to paste it, so add it as a new row. DataRow newrow = mainTable.CurrentTable.NewRow(); if (mainTable.MoveAndFreezeKeys) { // Data is being displayed with keys moved, so assume the clipboard data matches the visual appearance and not // the order of the data source. object tempitem; List <object> itemarray = line.Split('\t').Take(mainTable.EditedFile.CurrentType.Fields.Count).ToList <object>(); for (int i = mainTable.CurrentTable.PrimaryKey.Count() - 1; i >= 0; i--) { tempitem = itemarray[i]; itemarray.RemoveAt(i); itemarray.Insert(mainTable.CurrentTable.Columns.IndexOf(mainTable.CurrentTable.PrimaryKey[i]), tempitem); } // Once we have reordered the clipboard data to match the data source, convert to an object[] newrow.ItemArray = itemarray.ToArray(); } else { // Data is displayed as it is stored, so assume the clipboard data is ordered the same way. newrow.ItemArray = line.Split('\t').Take(mainTable.EditedFile.CurrentType.Fields.Count).ToArray <object>(); } mainTable.CurrentTable.Rows.Add(newrow); } rowindex++; continue; } if (String.IsNullOrEmpty(line.Trim())) { rowindex++; continue; } // Convert visual row and column index to data row and column index. datarowindex = mainTable.CurrentTable.Rows.IndexOf((mainTable.dbDataGrid.Items[rowindex] as DataRowView).Row); // Additional error checking for the visibleRows internal list. if (datarowindex >= mainTable._visibleRows.Count) { mainTable.UpdateVisibleRows(); } // Skip past any collapsed (filtered) rows. while (mainTable._visibleRows[datarowindex] == System.Windows.Visibility.Collapsed) { rowindex++; datarowindex = mainTable.CurrentTable.Rows.IndexOf((mainTable.dbDataGrid.Items[rowindex] as DataRowView).Row); if (rowindex >= mainTable.CurrentTable.Rows.Count) { break; } } foreach (string cell in line.Replace("\r", "").Split('\t')) { if (columnindex > mainTable.CurrentTable.Columns.Count - 1) { break; } if (String.IsNullOrEmpty(cell.Trim())) { columnindex++; continue; } // Convert visual column index, the display index not its location in the datagrid's collection, to data column index. datacolumnindex = mainTable.CurrentTable.Columns.IndexOf((string)mainTable.dbDataGrid.Columns.Single(n => n.DisplayIndex == columnindex).Header); // Since refresh works on the visual tree, and the visual tree is not affected by DisplayIndex, find the columns location // in the datagrid's column collection to pass on. int refreshcolumnindex = mainTable.dbDataGrid.Columns.IndexOf(mainTable.dbDataGrid.Columns.Single(n => n.DisplayIndex == columnindex)); // Rather than attempting to modify cells as we go, we should modify them in batches // since any kind of sorting may interfere with paste order in real time. pasteinstructions.Add(String.Format("{0};{1};{2};{3};{4}", datarowindex, rowindex, datacolumnindex, refreshcolumnindex, cell)); columnindex++; } rowindex++; } // Now that we have a list of paste instructions, execute them simultaneously across the data source // to avoid interference from any visual resorting. // Instruction Format: Data Row index;Visual Row index;Data Column index;Visual Column index;value foreach (string instruction in pasteinstructions) { // Parse out the instructions. string[] parameters = instruction.Split(';'); datarowindex = int.Parse(parameters[0]); rowindex = int.Parse(parameters[1]); datacolumnindex = int.Parse(parameters[2]); columnindex = int.Parse(parameters[3]); // Edit currentTable mainTable.CurrentTable.Rows[datarowindex].BeginEdit(); mainTable.CurrentTable.Rows[datarowindex][datacolumnindex] = parameters[4]; mainTable.CurrentTable.Rows[datarowindex].EndEdit(); // Refresh the visual cell mainTable.RefreshCell(rowindex, columnindex); } } else { // Paste Rows, with no floater cells. if (mainTable.dbDataGrid.SelectedCells.Count == (mainTable.dbDataGrid.SelectedItems.Count * mainTable.EditedFile.CurrentType.Fields.Count)) { // Only rows are selected. // Since the SelectedItems list is in the order of selection and NOT in the order of appearance we need // to create a custom sorted list of indicies to paste to. List <int> indiciesToPaste = new List <int>(); List <int> dataindiciesToPaste = new List <int>(); int testindex; foreach (DataRowView rowview in mainTable.dbDataGrid.SelectedItems.OfType <DataRowView>()) { testindex = mainTable.CurrentTable.Rows.IndexOf(rowview.Row); // Additional error checking for the visibleRows internal list. if (testindex >= mainTable._visibleRows.Count) { mainTable.UpdateVisibleRows(); } // Skip any collapsed (filtered) rows. if (mainTable._visibleRows[testindex] == System.Windows.Visibility.Visible) { indiciesToPaste.Add(mainTable.dbDataGrid.Items.IndexOf(rowview)); } } indiciesToPaste.Sort(); // Now that we have the selected rows visual locations, we need to determine their locations in our data source. foreach (int i in indiciesToPaste) { dataindiciesToPaste.Add(mainTable.CurrentTable.Rows.IndexOf((mainTable.dbDataGrid.Items[i] as DataRowView).Row)); } // We now have a list of data indicies sorted in visual order. int currentindex = 0; foreach (string line in clipboarddata.Replace("\r", "").Split('\n')) { if (!IsLineARow(mainTable, line) || String.IsNullOrEmpty(line) || !IsRowValid(mainTable, line)) { currentindex++; continue; } if (currentindex >= dataindiciesToPaste.Count) { // Add new row DataRow newrow = mainTable.CurrentTable.NewRow(); if (mainTable.MoveAndFreezeKeys) { // Data is being displayed with keys moved, so assume the clipboard data matches the visual appearance and not // the order of the data source. object tempitem; List <object> itemarray = line.Split('\t').Take(mainTable.EditedFile.CurrentType.Fields.Count).ToList <object>(); for (int i = mainTable.CurrentTable.PrimaryKey.Count() - 1; i >= 0; i--) { tempitem = itemarray[i]; itemarray.RemoveAt(i); itemarray.Insert(mainTable.CurrentTable.Columns.IndexOf(mainTable.CurrentTable.PrimaryKey[i]), tempitem); } // Once we have reordered the clipboard data to match the data source, convert to an object[] newrow.ItemArray = itemarray.ToArray(); } else { // Data is displayed as it is stored, so assume the clipboard data is ordered the same way. newrow.ItemArray = line.Split('\t').Take(mainTable.EditedFile.CurrentType.Fields.Count).ToArray <object>(); } mainTable.CurrentTable.Rows.Add(newrow); currentindex++; continue; } mainTable.CurrentTable.Rows[dataindiciesToPaste[currentindex]].BeginEdit(); if (mainTable.MoveAndFreezeKeys) { // Data is being displayed with keys moved, so assume the clipboard data matches the visual appearance and not // the order of the data source. object tempitem; List <object> itemarray = line.Split('\t').Take(mainTable.EditedFile.CurrentType.Fields.Count).ToList <object>(); for (int i = mainTable.CurrentTable.PrimaryKey.Count() - 1; i >= 0; i--) { tempitem = itemarray[i]; itemarray.RemoveAt(i); itemarray.Insert(mainTable.CurrentTable.Columns.IndexOf(mainTable.CurrentTable.PrimaryKey[i]), tempitem); } // Once we have reordered the clipboard data to match the data source, convert to an object[] mainTable.CurrentTable.Rows[dataindiciesToPaste[currentindex]].ItemArray = itemarray.ToArray(); } else { // Data is displayed as it is stored, so assume the clipboard data is ordered the same way. mainTable.CurrentTable.Rows[dataindiciesToPaste[currentindex]].ItemArray = line.Split('\t').Take(mainTable.EditedFile.CurrentType.Fields.Count).ToArray <object>(); } mainTable.CurrentTable.Rows[dataindiciesToPaste[currentindex]].EndEdit(); currentindex++; } mainTable.Refresh(true); } else { // Please select rows. MessageBox.Show("When pasting rows, please use the row header button to select entire rows only.", "Selection Error", MessageBoxButton.OK, MessageBoxImage.Warning); } } }