protected override System.Drawing.Size GetPreferredSize(System.Drawing.Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, System.Drawing.Size constraintSize) { if (ownerCell != null) { return(new System.Drawing.Size(0, 0)); } var size = base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize); var grid = DataGridView; int rangwidth = 0; foreach (int w in DataGridViewCellExHelper.Range(ColumnIndex + 1, columnSpan - 1)) { rangwidth += grid.Columns[w].Width; } var width = size.Width - rangwidth; int rangheight = 0; foreach (int w in DataGridViewCellExHelper.Range(RowIndex + 1, RowSpan - 1)) { rangheight += grid.Rows[w].Height; } var height = size.Height - rangheight; return(new System.Drawing.Size(width, height)); }
private void SetSpan(int columnspan, int rowspan) { int prevColumnSpan = columnSpan; int prevRowSpan = rowSpan; columnSpan = columnspan; rowSpan = rowspan; if (DataGridView != null) { //清除之前的合并 foreach (int rowIndex in DataGridViewCellExHelper.Range(RowIndex, prevRowSpan)) { foreach (int colIndex in DataGridViewCellExHelper.Range(ColumnIndex, prevColumnSpan)) { var cell = DataGridView[colIndex, rowIndex] as DataGridViewTextBoxCellEx; if (cell != null) { cell.OwnerCell = null; } } } //设置新的合并 foreach (int row in DataGridViewCellExHelper.Range(RowIndex, rowSpan)) { foreach (int col in DataGridViewCellExHelper.Range(ColumnIndex, columnSpan)) { var cell = DataGridView[col, row] as DataGridViewTextBoxCellEx; if (cell != null && cell != this) { if (cell.ColumnSpan > 1) { cell.ColumnSpan = 1; } if (cell.RowSpan > 1) { cell.RowSpan = 1; } cell.OwnerCell = this; } } } OwnerCell = null; DataGridView.Invalidate(); } }
private bool CellsRegionContainsSelectedCell(int columnIndex, int rowIndex, int columnSpan, int rowSpan) { if (DataGridView == null) { return(false); } foreach (int col in DataGridViewCellExHelper.Range(columnIndex, columnSpan)) { foreach (int row in DataGridViewCellExHelper.Range(rowIndex, rowSpan)) { if (DataGridView[col, row].Selected) { return(true); } } } return(false); }