效果
/// <summary>
/// DataGridView 单元格合并信息
/// </summary>
public struct DataGridViewMergeCellRegion
{
public int FromRowIndex { get; set; }
public int FromColIndex { get; set; }
public int ToRowIndex { get; set; }
public int ToColIndex { get; set; }
}
public class DataGridViewMergeColumn : DataGridViewColumn
{
public DataGridViewMergeColumn()
: base(new DataGridViewMergeCell())
{
}
public DataGridViewMergeColumn(DataGridViewCell cell)
: base(cell)
{
}
/// <summary>
/// 是否是进度
/// </summary>
public bool IsJingDu { get; set; }
}
/// <summary>
/// 如果列隐藏会有bug
/// </summary>
public class DataGridViewMergeCell : DataGridViewCell
{
int GetMergeWidth(bool all, DataGridViewMergeCellRegion reg)
{
int w = 0, lindex = reg.FromColIndex, lend = all ? reg.ToColIndex + 1 : this.ColumnIndex;
int rowindex = this.RowIndex;
DataGridViewCell cell;
while (lindex < lend)
{
cell = this.DataGridView.Rows[rowindex].Cells[lindex];
w += cell.Size.Width;
lindex += 1;
}
return w;
}
int GetMergeHeight(bool all, DataGridViewMergeCellRegion reg)
{
int w = 0, lindex = reg.FromRowIndex, lend = all ? reg.ToRowIndex + 1 : this.RowIndex;
int colindex = this.ColumnIndex;
DataGridViewCell cell;
while (lindex < lend)
{
cell = this.DataGridView.Rows[lindex].Cells[colindex];
w += cell.Size.Height;
lindex += 1;
}
return w;
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value == null || value == DBNull.Value || string.IsNullOrEmpty(cellStyle.Format))
{
if (value == null || value == DBNull.Value)
return "";
return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
return String.Format("{0:" + cellStyle.Format + "}", value);
}
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formatted