protected override void OnKeyDown(KeyEventArgs e) { //Expands row if right arrow is pressed if (e.KeyData == Keys.Right) { int totalRowsAdded = 0; foreach (var row in SelectedCells.Cast <DataGridViewCell>().Select(o => o.OwningRow).Distinct().OrderByDescending(o => o.Index)) { totalRowsAdded += expand(row.Index, expandedRowList[row.Index].RowIndexHierarchy.Count); } base.RowCount += totalRowsAdded; } //Collapses row if left arrow is pressed else if (e.KeyData == Keys.Left) { int totalRowsRemoved = 0; foreach (var row in SelectedCells.Cast <DataGridViewCell>().Select(o => o.OwningRow).Distinct().OrderByDescending(o => o.Index)) { totalRowsRemoved += collapse(row.Index); } base.RowCount -= totalRowsRemoved; } else { base.OnKeyDown(e); return; } Refresh(); }
public void addTrackInfo(List <Track> tracks) { IsImporting = true; List <int> rows = SelectedCells.Cast <DataGridViewCell>() .Select(x => x.RowIndex).Distinct().ToList <int>(); bool singleSelected = false; if (rows.Count == 0) { rows.Add(0); // start adding from first row if nothing is selected } if (rows.Count == 1) { for (int i = 1; i < tracks.Count; i++) { if (rows[rows.Count - 1] >= Rows.Count - 1) { break; } rows.Add(rows[rows.Count - 1] + 1); } singleSelected = true; } for (int i = 0; i < Math.Min(tracks.Count, rows.Count); i++) { Track track = tracks[i]; int r = rows[i]; this[pieceobj.Name, r].Value = track.Piece; this[title.Name, r].Value = track.Name; } this.Invalidate(); if (!this.Focused && singleSelected) { ClearSelection(); Rows[(rows[rows.Count - 1] + 1 >= Rows.Count ? Rows.Count - 1 : rows[rows.Count - 1] + 1)].Selected = true; FirstDisplayedScrollingRowIndex = (SelectedRows[0].Index - DisplayedRowCount(false) / 2 < 0) ? 0 : SelectedRows[0].Index - DisplayedRowCount(false) / 2; } IsImporting = false; }
protected override void OnKeyUp(KeyEventArgs e) { if ((e.Shift && e.KeyCode == Keys.F10) || e.KeyCode == Keys.Apps) { // pop context menu on Shift+F10 or context key (i.e. "Apps") e.Handled = true; e.SuppressKeyPress = false; var selectedCells = SelectedCells.Cast <DataGridViewCell>(); var cell = selectedCells.FirstOrDefault(dataGridViewCell => dataGridViewCell.Displayed); if (cell != null) { var rect = GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true); var bottomLeft = new Point(rect.Left, rect.Bottom); InvokeContextMenu(bottomLeft); } } else { base.OnKeyUp(e); } }
}//end OnPaste /// <summary> /// Clears the values of the selected cells. /// </summary> public void OnDelete() { //Get Selection Range ///DataGridViewSelectedCellCollection (selectedCells) doesn't implement a generic ///IEnumerable<DataGridViewCell>, just IEnumerable. ///To enumerate the values when they are of type 'Object', .Cast<DataGridViewCell> var selected = SelectedCells.Cast <DataGridViewCell>().ToList(); //LINQ method to get start and end indexes int startRow = selected.Min(x => x.RowIndex); int startCol = selected.Min(x => x.ColumnIndex); int endRow = selected.Max(x => x.RowIndex); int endCol = selected.Max(x => x.ColumnIndex); //"Delete" selected cells (by assigning them empty strings) for (int irow = startRow; irow < (endRow + 1); irow++) { for (int jcol = startCol; jcol < (endCol + 1); jcol++) { Rows[irow].Cells[jcol].Value = DBNull.Value; } } }
protected override void OnCellValueChanged(DataGridViewCellEventArgs e) { if (e.RowIndex == -1 || e.ColumnIndex == -1) { return; } Piece p; List <Artist> l; Track track; if (Columns[e.ColumnIndex] == pieceobj && (p = (Piece)this[pieceobj.Name, e.RowIndex].Value) != null) { this[composer.Name, e.RowIndex].Value = p.Composer.GetName(Artist.NameFormats.Last_First); this[genre.Name, e.RowIndex].Value = p.Genre; } else if (Columns[e.ColumnIndex] == performersobj && (l = (List <Artist>) this[performersobj.Name, e.RowIndex].Value) != null) { this[performer.Name, e.RowIndex].Value = string.Join(" / ", l.ConvertAll <string>( a => a.GetName(Artist.NameFormats.First_Last_Type)).ToArray()); } else if (Columns[e.ColumnIndex] == trackobj && (track = (Track)this[trackobj.Name, e.RowIndex].Value) != null) { this[disc_num.Name, e.RowIndex].Value = track.DiscNumber; this[track_num.Name, e.RowIndex].Value = track.TrackNumber; this[pieceobj.Name, e.RowIndex].Value = track.Piece; this[title.Name, e.RowIndex].Value = track.Name; this[performersobj.Name, e.RowIndex].Value = track.Performers; this[year.Name, e.RowIndex].Value = track.Year == 0 ? "" : track.Year.ToString(); this[filename.Name, e.RowIndex].Value = track.FileName; this[filesize.Name, e.RowIndex].Value = string.Format(new FileSizeFormatProvider(), "{0:fs}", track.Size * 1024); this[length.Name, e.RowIndex].Value = new TimeSpan(0, 0, track.Length).Duration().ToString().Substring(3, 5); } // the rest applies only when the user explicitly change values if (IsImporting) { return; } // set the same value for multiple selected cells in the same column var cells = SelectedCells.Cast <DataGridViewCell>().Where(x => (x.ColumnIndex == e.ColumnIndex)).ToList(); foreach (var cell in cells) { cell.Value = this[e.ColumnIndex, e.RowIndex].Value; } // automatically set the same property for the pieces in the same set if (ColumnsWithRelatedValues.Contains(Columns[e.ColumnIndex].Name)) { foreach (DataGridViewRow row in Rows) { if (((Piece)row.Cells[pieceobj.Name].Value).ID == ((Piece)this[pieceobj.Name, e.RowIndex].Value).ID) { row.Cells[e.ColumnIndex].Value = this[e.ColumnIndex, e.RowIndex].Value; } } } base.OnCellValueChanged(e); }
private void ProcessNewCtrlDown(bool shiftKey = false) { DataGridViewCell lastSelectedCell = SelectedCells.Cast <DataGridViewCell>().Where(p => p.OwningRow.Frozen == false).OrderByDescending( p => p.RowIndex).FirstOrDefault(); if (lastSelectedCell == null) { return; } bool reverse = false; if (CurrentCell.RowIndex == lastSelectedCell.RowIndex && SelectedCells.Count > 1) { lastSelectedCell = SelectedCells.Cast <DataGridViewCell>().OrderBy(p => p.RowIndex).FirstOrDefault(); if (lastSelectedCell == null) { return; } reverse = true; } int currentColumnIndex = lastSelectedCell.ColumnIndex; int currentRowIndex = lastSelectedCell.RowIndex; DataGridViewCell nextCell = Rows.Count == lastSelectedCell.RowIndex + 1 ? null : Rows[lastSelectedCell.RowIndex + 1].Cells[currentColumnIndex]; if (nextCell == null) { return; } DataGridViewCell targetCell; if ((lastSelectedCell.Value != null && nextCell.Value != null) && (lastSelectedCell.Value.ToString() == string.Empty || (lastSelectedCell.Value.ToString() != string.Empty && nextCell.Value.ToString() == string.Empty))) { targetCell = nextCell.Value.ToString() == string.Empty ? Rows.Cast <DataGridViewRow>().Where( p => p.Cells[currentColumnIndex].Value != null && p.Frozen == false && p.Cells[currentColumnIndex].RowIndex > currentRowIndex && p.Cells[currentColumnIndex].Value.ToString() != string.Empty).Select( p => p.Cells[currentColumnIndex]).FirstOrDefault() : nextCell; //no non blank exists, so goto the last cell in that column targetCell = targetCell ?? Rows[Rows.Count - 1].Cells[currentColumnIndex]; targetCell = targetCell ?? Rows.Cast <DataGridViewRow>().OrderByDescending(p => p.Cells[currentColumnIndex].RowIndex). First().Cells[currentColumnIndex]; } else { targetCell = Rows.Cast <DataGridViewRow>().Where( p => p.Cells[currentColumnIndex].Value != null && p.Frozen == false && p.Cells[currentColumnIndex].RowIndex > currentRowIndex && p.Cells[currentColumnIndex].Value.ToString() == string.Empty).Select( p => p.Cells[currentColumnIndex]).FirstOrDefault(); targetCell = targetCell == null ? Rows[Rows.Count - 1].Cells[currentColumnIndex] : Rows[targetCell.RowIndex - 1].Cells[currentColumnIndex]; } if (shiftKey) { int selectedRowIndex = reverse ? SelectedCells.Cast <DataGridViewCell>().OrderByDescending(p => p.RowIndex). First().RowIndex : SelectedCells.Cast <DataGridViewCell>().OrderBy(p => p.RowIndex).First(). RowIndex; List <DataGridViewCell> toBeSelectedRows = reverse ? Rows.Cast <DataGridViewRow>().Where( p => p.Cells[currentColumnIndex].Value != null && p.Frozen == false && p.Cells[currentColumnIndex].RowIndex >= selectedRowIndex && p.Cells[currentColumnIndex].RowIndex <= targetCell.RowIndex).Select( p => p.Cells[currentColumnIndex]). OrderByDescending(p => p.RowIndex).ToList() : Rows.Cast <DataGridViewRow>().Where( p => p.Cells[currentColumnIndex].Value != null && p.Frozen == false && p.Cells[currentColumnIndex].RowIndex >= selectedRowIndex && p.Cells[currentColumnIndex].RowIndex <= targetCell.RowIndex).Select( p => p.Cells[currentColumnIndex]).OrderBy( p => p.RowIndex).ToList(); SelectedCells.Cast <DataGridViewCell>().Where(p => p.RowIndex < targetCell.RowIndex).ForEach( p => p.Selected = p.RowIndex > CurrentCell.RowIndex); toBeSelectedRows.ForEach(a => a.Selected = CurrentCell.RowIndex <= a.RowIndex); targetCell.Selected = true; if (FirstDisplayedScrollingRowIndex + DisplayedRowCount(false) <= targetCell.RowIndex) { FirstDisplayedScrollingRowIndex = targetCell.RowIndex - DisplayedRowCount(false) + 1; } } else { CurrentCell = targetCell; } }
private void ProcessNewCtrlUp(bool shiftKey = false) { DataGridViewCell firstSelectedCell = SelectedCells.Cast <DataGridViewCell>().Where(p => p.OwningRow.Frozen == false).OrderBy(p => p.RowIndex). FirstOrDefault(); if (firstSelectedCell == null) { return; } bool reverse = false; if (CurrentCell.RowIndex == firstSelectedCell.RowIndex && SelectedCells.Count > 1) { firstSelectedCell = SelectedCells.Cast <DataGridViewCell>().Where(p => p.OwningRow.Frozen == false).OrderByDescending( p => p.RowIndex).FirstOrDefault(); if (firstSelectedCell == null) { return; } reverse = true; } int currentColumnIndex = firstSelectedCell.ColumnIndex; int currentRowIndex = firstSelectedCell.RowIndex; DataGridViewCell beforeCell = firstSelectedCell.RowIndex <= 0 ? null : Rows[firstSelectedCell.RowIndex - 1].Cells[currentColumnIndex]; if (beforeCell == null) { return; } DataGridViewCell targetCell; if ((firstSelectedCell.Value != null && beforeCell.Value != null) && (firstSelectedCell.Value.ToString() == string.Empty || (firstSelectedCell.Value.ToString() != string.Empty && beforeCell.Value.ToString() == string.Empty))) { targetCell = beforeCell.Value.ToString() == string.Empty ? Rows.Cast <DataGridViewRow>().Reverse().Where( p => p.Cells[currentColumnIndex].Value != null && p.Frozen == false && p.Cells[currentColumnIndex].RowIndex < currentRowIndex && p.Cells[currentColumnIndex].Value.ToString() != string.Empty).Select( p => p.Cells[currentColumnIndex]).FirstOrDefault() : beforeCell; //no non blank exists, so goto the last cell in that column targetCell = targetCell ?? Rows[0].Cells[currentColumnIndex]; } else { targetCell = Rows.Cast <DataGridViewRow>().Reverse().Where( p => p.Cells[currentColumnIndex].Value != null && p.Frozen == false && p.Cells[currentColumnIndex].RowIndex < currentRowIndex && p.Cells[currentColumnIndex].Value.ToString() == string.Empty).Select( p => p.Cells[currentColumnIndex]).FirstOrDefault(); //targetCell = targetCell == null ? Rows[0].Cells[currentColumnIndex] : Rows[targetCell.RowIndex + 1].Cells[currentColumnIndex]; targetCell = targetCell == null ? Rows.Cast <DataGridViewRow>().Where(p => p.Frozen == false).Select( p => p.Cells[currentColumnIndex]).FirstOrDefault() : Rows[targetCell.RowIndex + 1].Cells[currentColumnIndex]; } if (targetCell == null) { return; } if (shiftKey) { int selectedRowIndex = reverse ? SelectedCells.Cast <DataGridViewCell>().OrderByDescending(p => p.RowIndex). First().RowIndex : SelectedCells.Cast <DataGridViewCell>().OrderBy(p => p.RowIndex).First(). RowIndex; List <DataGridViewCell> toBeSelectedRows = reverse ? Rows.Cast <DataGridViewRow>().Where( p => p.Frozen == false && p.Cells[currentColumnIndex].RowIndex <= selectedRowIndex && p.Cells[currentColumnIndex].RowIndex >= targetCell.RowIndex).Select( p => p.Cells[currentColumnIndex]).OrderBy( p => p.RowIndex).ToList() : Rows.Cast <DataGridViewRow>().Where( p => p.Frozen == false && p.Cells[currentColumnIndex].RowIndex <= selectedRowIndex && p.Cells[currentColumnIndex].RowIndex >= targetCell.RowIndex).Select( p => p.Cells[currentColumnIndex]). OrderByDescending(p => p.RowIndex).ToList(); toBeSelectedRows.ForEach(a => a.Selected = CurrentCell.RowIndex >= a.RowIndex); targetCell.Selected = true; if (FirstDisplayedScrollingRowIndex > targetCell.RowIndex) { FirstDisplayedScrollingRowIndex = targetCell.RowIndex; } } else { CurrentCell = targetCell; } }