private void replaceWindow_Replace(object sender, EventArgs e) { // If nothing is selected, then find something to replace first. if (_parentDbEdtiorTable.dbDataGrid.SelectedCells.Count == 0) { // If we fail to find a match, return. if (!FindNext(_findReplaceWindow.FindValue)) { return; } } // If nothing is STILL selected, then we found nothing to replace. // Or, if more than 1 cell is selected, we have a problem. if (_parentDbEdtiorTable.dbDataGrid.SelectedCells.Count == 0 || _parentDbEdtiorTable.dbDataGrid.SelectedCells.Count > 1) { return; } int rowindex = _parentDbEdtiorTable.dbDataGrid.Items.IndexOf(_parentDbEdtiorTable.dbDataGrid.SelectedCells.First().Item); int colindex = _parentDbEdtiorTable.dbDataGrid.Columns.IndexOf(_parentDbEdtiorTable.dbDataGrid.SelectedCells.First().Column); while (_findReplaceWindow.ReplaceValue.Equals(_parentDbEdtiorTable.CurrentTable.Rows[rowindex][colindex].ToString())) { // If what is selected has already been replaced, move on to the next match, returning if we fail. if (!FindNext(_findReplaceWindow.FindValue)) { return; } // Update current coordinates. rowindex = _parentDbEdtiorTable.dbDataGrid.Items.IndexOf(_parentDbEdtiorTable.dbDataGrid.SelectedCells.First().Item); colindex = _parentDbEdtiorTable.dbDataGrid.Columns.IndexOf(_parentDbEdtiorTable.dbDataGrid.SelectedCells.First().Column); } if (_findReplaceWindow.FindValue.Equals(_parentDbEdtiorTable.CurrentTable.Rows[rowindex][colindex].ToString())) { // Test for a combobox comlumn. if (_parentDbEdtiorTable.dbDataGrid.Columns[colindex] is DataGridComboBoxColumn) { if (ComboBoxColumnContainsValue((DataGridComboBoxColumn)_parentDbEdtiorTable.dbDataGrid.Columns[colindex], _findReplaceWindow.ReplaceValue)) { // The value in the Replace field is not valid for this column, alert user and return. MessageBox.Show(String.Format("The value '{0}', is not a valid value for Column '{1}'", _findReplaceWindow.ReplaceValue, (string)_parentDbEdtiorTable.dbDataGrid.Columns[colindex].Header)); return; } } // Assign the value, and update the UI _parentDbEdtiorTable.CurrentTable.Rows[rowindex][colindex] = _findReplaceWindow.ReplaceValue; _parentDbEdtiorTable.RefreshCell(rowindex, colindex); } }
public static void DataGridMassEditStringsMenuItem(object sender, DBTableControl.DBEditorTableControl mainTable) { InputBox stringeditbox = new InputBox(); stringeditbox.Input = "{cell}"; stringeditbox.ShowDialog(); if (stringeditbox.DialogResult == System.Windows.Forms.DialogResult.OK) { int datarowindex = -1; int datacolindex = -1; foreach (DataGridCellInfo cellinfo in mainTable.dbDataGrid.SelectedCells.Where(n => n.Item is DataRowView)) { datarowindex = mainTable.CurrentTable.Rows.IndexOf((cellinfo.Item as DataRowView).Row); datacolindex = mainTable.CurrentTable.Columns.IndexOf((string)cellinfo.Column.Header); // Make sure our visible rows are up to date and skip any currently filtered rows. if (mainTable._visibleRows.Count <= mainTable.CurrentTable.Rows.Count) { mainTable.UpdateVisibleRows(); } if (mainTable._visibleRows[datarowindex] == System.Windows.Visibility.Collapsed) { continue; } mainTable.CurrentTable.Rows[datarowindex][datacolindex] = stringeditbox.Input.Replace("{cell}", mainTable.CurrentTable.Rows[datarowindex][datacolindex].ToString()); // Refresh the cell in the UI, using its visual coordinates. mainTable.RefreshCell(mainTable.dbDataGrid.Items.IndexOf(cellinfo.Item), mainTable.dbDataGrid.Columns.IndexOf(cellinfo.Column)); } } }
public static void ColumnMassEdit(object sender, DBTableControl.DBEditorTableControl mainTable) { DataGridColumn col = (((sender as MenuItem).Parent as ContextMenu).PlacementTarget as DataGridColumnHeader).Column; string colname = (string)col.Header; InputBox stringeditbox = new InputBox(); stringeditbox.Input = "{cell}"; stringeditbox.ShowDialog(); if (stringeditbox.DialogResult == System.Windows.Forms.DialogResult.OK) { int datacolindex = mainTable.CurrentTable.Columns.IndexOf(colname); for (int i = 0; i < mainTable.CurrentTable.Rows.Count; i++) { // Make sure our visible rows are up to date and skip any currently filtered rows. if (mainTable._visibleRows.Count <= mainTable.CurrentTable.Rows.Count) { mainTable.UpdateVisibleRows(); } if (mainTable._visibleRows[i] == System.Windows.Visibility.Collapsed) { continue; } mainTable.CurrentTable.Rows[i][datacolindex] = stringeditbox.Input.Replace("{cell}", mainTable.CurrentTable.Rows[i][datacolindex].ToString()); // Refresh the cell in the UI, using its visual coordinates. mainTable.RefreshCell(mainTable.dbDataGrid.Items.IndexOf(mainTable.CurrentTable.DefaultView[i]), mainTable.dbDataGrid.Columns.IndexOf(col)); } } }
public static void DataGridApplyExpressionMenuItem(object sender, DBTableControl.DBEditorTableControl mainTable) { ApplyExpressionWindow getexpwindow = new ApplyExpressionWindow(); getexpwindow.ShowDialog(); if (getexpwindow.DialogResult != null && (bool)getexpwindow.DialogResult) { foreach (DataGridCellInfo cellinfo in mainTable.dbDataGrid.SelectedCells) { // Determine current cells indecies, row and column int columnindex = mainTable.CurrentTable.Columns.IndexOf((string)cellinfo.Column.Header); int rowindex = mainTable.CurrentTable.Rows.IndexOf((cellinfo.Item as DataRowView).Row); // Skip any filtered rows, or any rows with an error in the column we are computing. mainTable.UpdateVisibleRows(); if (mainTable._visibleRows[rowindex] == System.Windows.Visibility.Collapsed || mainTable._errorList.Count(n => n.RowIndex == rowindex && n.ColumnName.Equals((string)cellinfo.Column.Header)) > 0) { continue; } // Get the expression, replacing x for the current cell's value. string expression = getexpwindow.EnteredExpression.Replace("x", string.Format("{0}", mainTable.CurrentTable.Rows[rowindex][columnindex])); // Compute spits out the new value after the current value is applied to the expression given. object newvalue = mainTable.CurrentTable.Compute(expression, ""); // For integer based columns, do a round first if necessary. if (mainTable.EditedFile.CurrentType.Fields[columnindex].TypeCode == TypeCode.Int32 || mainTable.EditedFile.CurrentType.Fields[columnindex].TypeCode == TypeCode.Int16) { int newintvalue; if (!Int32.TryParse(newvalue.ToString(), out newintvalue)) { double tempvalue = Double.Parse(newvalue.ToString()); tempvalue = Math.Round(tempvalue, 0); newintvalue = (int)tempvalue; } newvalue = newintvalue; } mainTable.CurrentTable.Rows[rowindex][columnindex] = newvalue; // Refresh the cell in the UI, using its visual coordinates. mainTable.RefreshCell(mainTable.dbDataGrid.Items.IndexOf(cellinfo.Item), mainTable.dbDataGrid.Columns.IndexOf(cellinfo.Column)); } } }
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); } } }