public void DataGridViewSelectedCellCollection_CopyTo_InvokeEmpty_Success() { using var control = new DataGridView(); DataGridViewSelectedCellCollection collection = control.SelectedCells; var array = new object[] { 1, 2, 3 }; collection.CopyTo(array, 1); Assert.Equal(new object[] { 1, 2, 3 }, array); }
/// <summary> /// 将选中的单元格的数据同步到该单元格所属的行或列 /// </summary> /// <param name="cells">单元格集合</param> /// <param name="type">0-行 1-列 2全部</param> public static void cellsDataCopyToRowOrCol(DataGridView dataGrid, int type) { DataGridViewSelectedCellCollection selCells = dataGrid.SelectedCells; // 将选中单元格集合变为数组 DataGridViewCell[] cells = new DataGridViewCell[selCells.Count]; selCells.CopyTo(cells, 0); // 执行同步 cellsDataCopyToRowOrCol(cells, type); }
public void DataGridViewSelectedCellCollection_CopyTo_InvokeNotEmpty_ReturnsExpected() { using var control = new DataGridView { RowCount = 3, ColumnCount = 1, SelectionMode = DataGridViewSelectionMode.CellSelect }; control.Rows[0].Cells[0].Selected = true; control.Rows[1].Cells[0].Selected = false; control.Rows[2].Cells[0].Selected = true; DataGridViewSelectedCellCollection collection = control.SelectedCells; var array = new object[] { 1, 2, 3 }; collection.CopyTo(array, 1); Assert.Equal(new object[] { 1, control.Rows[2].Cells[0], control.Rows[0].Cells[0] }, array); }
private void InitStructure(DataGridViewSelectedCellCollection Cells) { if (Cells != null) { selected = new DataGridViewCell[Cells.Count]; values = new string[selected.Length]; Cells.CopyTo(selected, 0); Array.Sort(selected, new Comparer()); for (int index = 0; index < values.Length; index++) { values[index] = selected[index].Value; } native = new object[values.Length]; values.CopyTo(native, 0); int minColumn = int.MaxValue, maxColumn = int.MinValue; RowsCount = selected[selected.Length - 1].RowIndex - selected[0].RowIndex + 1; foreach (DataGridViewCell cell in selected) { if (cell.ColumnIndex > maxColumn) { maxColumn = cell.ColumnIndex; } if (cell.ColumnIndex < minColumn) { minColumn = cell.ColumnIndex; } } ColumnsCount = maxColumn - minColumn + 1; } }