/// <summary> /// Calculate the 'SmoothInfo' for a selected set of cells. /// </summary> private SmoothInfo GetSmoothInfo(double min, double max) { DataGridViewSelectedCellCollection selected = this.dataGrid.SelectedCells; if (this.SelectedColumn(selected)) { SmoothInfo result = new SmoothInfo(); result.MinValue = min; result.MaxValue = max; IEnumerable <DataGridViewCell> cells = selected.Cast <DataGridViewCell>(); int minY = cells.Min(cell => cell.RowIndex); int maxY = cells.Max(cell => cell.RowIndex); result.A = cells.Where(cell => cell.RowIndex == minY).First(); result.B = cells.Where(cell => cell.RowIndex == maxY).First(); return(result); } if (this.SelectedRow(this.dataGrid.SelectedCells)) { SmoothInfo result = new SmoothInfo(); result.MinValue = min; result.MaxValue = max; IEnumerable <DataGridViewCell> cells = selected.Cast <DataGridViewCell>(); int minX = cells.Min(cell => cell.ColumnIndex); int maxX = cells.Max(cell => cell.ColumnIndex); result.A = cells.Where(cell => cell.ColumnIndex == minX).First(); result.B = cells.Where(cell => cell.ColumnIndex == maxX).First(); return(result); } return(null); }
private void terminateToolStripMenuItem_Click(object sender, EventArgs e) { try { DialogResult result; result = DisplayQuestion("Are you sure you want to terminate selected service instance(s)?"); if (result == DialogResult.OK) { Instances = new List <DataGridViewCell>(); DataGridViewSelectedCellCollection instances = instancesGridView.SelectedCells; var q = from instance in instances.Cast <DataGridViewCell>() where !String.IsNullOrEmpty(instance.FormattedValue.ToString()) && instance.OwningColumn.DataPropertyName == Constants._SERVICE_INSTANCE_ID select instance; Instances = q.ToList(); progressBar1.Value = 0; terminateWorkerIndex = 0; swinstancesAction = new Stopwatch(); swinstancesAction.Start(); progressBar1.Maximum = this.Instances.Count; List <BaseAction> actions = new List <BaseAction>(); foreach (DataGridViewCell val in Instances) { actions.Add(new TerminateInstancesAction(val.FormattedValue.ToString())); } BeginTerminating(actions); } } catch (Exception ex) { DisplayError(ex); } }
private void ChangeCellColorToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog colorDialog = new ColorDialog(); DataGridViewSelectedCellCollection cellCollection = dataGridView1.SelectedCells; IEnumerable<DataGridViewCell> cellList = cellCollection.Cast<DataGridViewCell>(); List<DataGridViewCell> dgvcList = cellList.ToList(); List<Cell> undoList = new List<Cell>(); // create list of cells that are being changed for (int j = 0; j < dgvcList.Count; j++) { int column = dgvcList[j].ColumnIndex; int row = dgvcList[j].RowIndex; undoList.Add(sheet.GetCell(column, row)); } // push list to undo stack sheet.AddtoUndoStack(undoList); if (colorDialog.ShowDialog() == DialogResult.OK) { Color newColor = colorDialog.Color; // change color of selected cells based on dialog input for (int i = 0; i < dgvcList.Count; i++) { DataGridViewCell cell = dgvcList[i]; ChangeCellColor(cell, newColor); } } }
private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult resultMsg; resultMsg = DisplayWarning("Data contained in messages may be confidential. Make sure the destination you choose is secure."); try { if (resultMsg == DialogResult.OK) { if (folderSaveMessagesBrowserDialog.ShowDialog() == DialogResult.OK) { saveInstancesResult = new StringBuilder(); Instances = new List <DataGridViewCell>(); DataGridViewSelectedCellCollection instances = instancesGridView.SelectedCells; var q = from instance in instances.Cast <DataGridViewCell>() where !String.IsNullOrEmpty(instance.FormattedValue.ToString()) && instance.OwningColumn.DataPropertyName == Constants._SERVICE_INSTANCE_ID select instance; Instances = q.ToList(); progressBar1.Value = 0; terminateWorkerIndex = 0; swinstancesAction = new Stopwatch(); swinstancesAction.Start(); progressBar1.Maximum = this.Instances.Count; List <BaseAction> actions = new List <BaseAction>(); foreach (DataGridViewCell val in Instances) { actions.Add(new SaveInstancesAction(val.FormattedValue.ToString(), folderSaveMessagesBrowserDialog.SelectedPath)); } BeginSavingInstances(actions); } } } catch (Exception ex) { DisplayError(ex); } }
async private void btnFIFO_Click(object sender, EventArgs e) { if (resultList.Count == 0) { return; } if (FIFOProcessing) { FIFOProcessing = false; btnFIFO.BackColor = DefaultColor; return; } bool isSelection; List <DataGridViewCell> targetCells; DataGridViewSelectedCellCollection selected = dgv.SelectedCells; if (selected.Count > 1) { targetCells = selected.Cast <DataGridViewCell>().OrderBy(n => n.RowIndex).ToList(); isSelection = true; } else { targetCells = dgv.Rows.Cast <DataGridViewRow>().Select(n => n.Cells[0]).ToList(); isSelection = false; } foreach (DataGridViewCell cell in targetCells) { cell.Style.BackColor = Color.LightGray; } DefaultColor = btnFIFO.BackColor; btnFIFO.BackColor = Color.Gray; FIFOProcessing = true; foreach (DataGridViewCell cell in targetCells) { if (!FIFOProcessing) { MessageBox.Show("キャンセルされました。"); break; } dgv.ClearSelection(); cell.Selected = true; if (!cell.Displayed) { dgv.FirstDisplayedScrollingRowIndex = cell.RowIndex; } string line = cell.Value.ToString(); string prefix = txtPrefix.Text; string suffix = txtSuffix.Text; Clipboard.SetText($"{prefix}{line}{suffix}"); cell.Style.BackColor = Color.White; await Task.Delay(500); } FIFOProcessing = false; btnFIFO.BackColor = DefaultColor; }
//Metodo encargado de realizar el pegado de lo copiado en el clipboard public static void pasteClipboard(DataGridView dataGridView) { try { //Obtener la data del clipboard en forma de matriz string[,] clipboardDataMatrix = getMatrixDataFromClipboard(); int clipboardDataMatrixRows = clipboardDataMatrix.GetLength(0); int clipboardDataMatrixColumns = clipboardDataMatrix.GetLength(1); //Obtener info de las celdas seleccionadas para pegar DataGridViewSelectedCellCollection selectedCells = dataGridView.SelectedCells; int selectedCellsRows = selectedCells.Cast <DataGridViewCell>().Select(x => x.RowIndex).Distinct().Count(); int selectedCellsColumns = selectedCells.Cast <DataGridViewCell>().Select(x => x.ColumnIndex).Distinct().Count(); int selectedCellsStartRow = selectedCells.Cast <DataGridViewCell>().Select(x => x.RowIndex).Distinct().Min(); int selectedCellsStartColumn = selectedCells.Cast <DataGridViewCell>().Select(x => x.ColumnIndex).Distinct().Min(); //Obtener info de las DataGridView int dataGridViewRows = dataGridView.RowCount; int dataGridViewColumns = dataGridView.ColumnCount; //Comparar el tamaño de la data copiada con el tamaño de las celdas seleccionadas //Si copio una sola celda if (clipboardDataMatrixRows == 1 && clipboardDataMatrixColumns == 1) { string copiedValue = clipboardDataMatrix[0, 0]; foreach (DataGridViewCell selectedCell in selectedCells) { if (!selectedCell.ReadOnly) { selectedCell.Value = copiedValue; } } } //Si copio mas de una celda if (clipboardDataMatrix.Length > 1) { //Y tengo la misma matriz seleccionada bool sameSelectedMatixSize = clipboardDataMatrixRows == selectedCellsRows && clipboardDataMatrixColumns == selectedCellsColumns; //O tengo una sola celda seleccionada bool selectedOnlyOneCell = selectedCellsRows == 1 && selectedCellsColumns == 1; if (sameSelectedMatixSize || selectedOnlyOneCell) { //Verificar que haya espacio para pegar bool thereAreEnoughCells = selectedCellsStartRow + clipboardDataMatrixRows <= dataGridViewRows && selectedCellsStartColumn + clipboardDataMatrixColumns <= dataGridViewColumns; if (thereAreEnoughCells) { for (int i = 0; i < clipboardDataMatrixRows; i++) { for (int j = 0; j < clipboardDataMatrixColumns; j++) { int rowIndex = selectedCellsStartRow + i; int columnIndex = selectedCellsStartColumn + j; if (!dataGridView[columnIndex, rowIndex].ReadOnly) { dataGridView[columnIndex, rowIndex].Value = clipboardDataMatrix[i, j]; } } } } } } } catch (Exception ex) { MessageBox.Show("There was a problem pasting clipboard data: " + ex.Message); } }