private void fomr1_ClipBoardChanged(object sender, ClipboardListnerArgs e) { Clips.BoardDataTable clips = dataGridView1.DataSource as Clips.BoardDataTable; if (clips != null) { string text = (e.Text ?? string.Empty).Trim(); if (clips != null && !string.IsNullOrEmpty(text)) { Clips.BoardRow row = clips.NewBoardRow(); row.BoardText = text; bool exists = false; for (int i = 0; i < clips.Count; i++) { if (clips[i].BoardText == row.BoardText) { if (i > 0) { clips.Rows.RemoveAt(i); clips.Rows.InsertAt(row, 0); } exists = true; dataGridView1.CurrentCell = dataGridView1[(int)GridColumns.BoardText, 0]; } } if (!exists && !bSwitch.Checked) { clips.Rows.InsertAt(row, 0); dataGridView1.CurrentCell = dataGridView1[(int)GridColumns.BoardText, 0]; } dataGridView1.Invalidate(); } } }
private static void UpDownOrDelete(bool?isUp, DataGridView dataGridView1) { Clips.BoardDataTable clips = null; if (dataGridView1 == null || dataGridView1.SelectedCells == null || dataGridView1.SelectedCells.Count < 1 || (clips = dataGridView1.DataSource as Clips.BoardDataTable) == null) { return; } dataGridView1.SuspendLayout(); Dictionary <int, int> idxs = new Dictionary <int, int>(); for (int i = 0; i < dataGridView1.SelectedCells.Count; i++) { idxs.Add(dataGridView1.SelectedCells[i].RowIndex, -1); } List <int> rows = new List <int>(); foreach (int id in idxs.Keys) { rows.Add(id); } for (int i = rows.Count - 1; i >= 0; i--) { if (rows[i] < 0 || rows[i] >= dataGridView1.RowCount) { return; } } rows.Sort(); if (isUp == null) { for (int i = rows.Count - 1; i >= 0; i--) { clips.Rows.RemoveAt(rows[i]); } } else { int diff = rows[rows.Count - 1] - rows[0]; int plus = 1; int temp = 0; for (int i = 1; i < rows.Count; i++) { if (Math.Abs(rows[i - 1] - rows[i]) == 1) { temp++; if (i != rows.Count - 1) { continue; } } if (plus < temp) { plus = temp; } temp = 0; } diff += plus; for (int i = 0; i < rows.Count; i++) { idxs[rows[i]] = rows[i] + (isUp == true ? -diff : diff); } for (int i = 0; i < rows.Count; i++) { if (idxs[rows[i]] < 0 || idxs[rows[i]] >= dataGridView1.RowCount) { return; } } List <Clips.BoardRow> boards = new List <Clips.BoardRow>(); for (int i = rows.Count - 1; i >= 0; i--) { Clips.BoardRow board = clips.NewBoardRow(); board.ItemArray = clips[rows[i]].ItemArray; boards.Add(board); clips.Rows.RemoveAt(rows[i]); } for (int i = 0; i < boards.Count; i++) { clips.Rows.InsertAt(boards[boards.Count - i - 1], idxs[rows[i]]); } for (int i = 0; i < dataGridView1.SelectedCells.Count; i++) { dataGridView1.SelectedCells[i--].Selected = false; } for (int i = 0; i < rows.Count; i++) { dataGridView1[(int)GridColumns.BoardText, idxs[rows[i]]].Selected = true; } } dataGridView1.Invalidate(); dataGridView1.ResumeLayout(); }