订单金额统计利润计算功能
This commit is contained in:
1 parent
eca6a8a9e7
commit
cc539ce604
27 files changed
+2090
-214
No files matched your search
@@ -237,5 +237,179 @@ namespace TemuKJD.Extension
|
||||
|
||||
return checkBox;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绘制合并的表头单元格
|
||||
/// </summary>
|
||||
/// <param name="dgv"></param>
|
||||
/// <param name="colNameCollection">单元格名称</param>
|
||||
/// <param name="headerText">合并后的表头显示文字</param>
|
||||
/// <param name="isEnableResizing">是否允许调整大小</param>
|
||||
public static void MergeColumnHeader(this DataGridView dgv, List<string> colNameCollection, string headerText, bool isMultiLevelHeader = false)
|
||||
{
|
||||
int nHeight = 0;
|
||||
int nLeft = 0;
|
||||
int nTop = 0;
|
||||
int nWidth = 0;
|
||||
bool isMultiLevel = isMultiLevelHeader; // 是否为二级复合表头
|
||||
bool isResize = false; // 是否已调整过表头
|
||||
|
||||
//合并列中某一列的列宽变化时,强制重绘表头
|
||||
dgv.ColumnWidthChanged += (s, e) =>
|
||||
{
|
||||
if (colNameCollection.Contains(e.Column.Name))
|
||||
{
|
||||
foreach(var columnName in colNameCollection)
|
||||
{
|
||||
dgv.InvalidateCell(dgv.Columns[columnName].HeaderCell); // 标记单元格为需要重绘
|
||||
}
|
||||
dgv.Update();
|
||||
}
|
||||
};
|
||||
dgv.Scroll += (s, e) =>
|
||||
{
|
||||
if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
|
||||
{
|
||||
bool tag = false;
|
||||
foreach (var columnName in colNameCollection)
|
||||
{
|
||||
var bounds = dgv.Columns[columnName].HeaderCell.ContentBounds;
|
||||
if (bounds.Left < 10 || bounds.Right > dgv.Width - 10)
|
||||
{
|
||||
tag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (tag == true)
|
||||
{
|
||||
foreach (var columnName in colNameCollection)
|
||||
{
|
||||
dgv.InvalidateCell(dgv.Columns[columnName].HeaderCell); // 标记单元格为需要重绘
|
||||
}
|
||||
dgv.Update();
|
||||
}
|
||||
}
|
||||
};
|
||||
//处理重绘事件,绘制合并表头和二级表头
|
||||
dgv.CellPainting += (s, e) =>
|
||||
{
|
||||
if (e.RowIndex == -1 && e.ColumnIndex >=0) // 重绘表头单元格及标题
|
||||
{
|
||||
string name = dgv.Columns[e.ColumnIndex].Name;
|
||||
// 调整为复合表头
|
||||
if (isMultiLevel && !isResize)
|
||||
{
|
||||
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
|
||||
dgv.ColumnHeadersHeight = e.CellBounds.Height * 2;
|
||||
isResize = true;
|
||||
}
|
||||
if (colNameCollection.Contains(name))
|
||||
{
|
||||
e.PaintBackground(e.CellBounds, false); // false不绘制单元格背景
|
||||
//e.PaintContent(e.CellBounds); // 绘制单元格内容,此处不要绘制
|
||||
|
||||
Brush brush;
|
||||
Pen pen;
|
||||
if (colNameCollection.IndexOf(name) == 0)
|
||||
{
|
||||
nTop = e.CellBounds.Top;
|
||||
nLeft = e.CellBounds.Left;
|
||||
nWidth = e.CellBounds.Width;
|
||||
if (isMultiLevelHeader)
|
||||
nHeight = e.CellBounds.Height / 2;
|
||||
else
|
||||
nHeight = e.CellBounds.Height;
|
||||
foreach (string str2 in colNameCollection)
|
||||
{
|
||||
if (!str2.Equals(name))
|
||||
{
|
||||
nWidth += dgv.Columns[str2].Width;
|
||||
}
|
||||
}
|
||||
}
|
||||
//填充表头背景(覆盖原来的分隔线)
|
||||
Rectangle rtTmp = new Rectangle(nLeft, nTop, nWidth, nHeight);
|
||||
using (brush = new SolidBrush(/*e.CellStyle.BackColor*/dgv.BackgroundColor))
|
||||
{
|
||||
var rtBounds = e.CellBounds;
|
||||
rtBounds.Height -= 1;
|
||||
e.Graphics.FillRectangle(brush, rtBounds);
|
||||
}
|
||||
//非多级表头,只需绘制最后一个连续表头的垂直分隔线
|
||||
if (!isMultiLevelHeader)
|
||||
{
|
||||
if (colNameCollection.IndexOf(name) == colNameCollection.Count - 1)
|
||||
{
|
||||
// 分隔线
|
||||
using (pen = new Pen(Color.FromArgb(229, 229, 229)))
|
||||
{
|
||||
var rtBounds = e.CellBounds;
|
||||
e.Graphics.DrawLine(pen, rtBounds.Right - 1, rtBounds.Top + 1, rtBounds.Right - 1, rtBounds.Bottom - 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//分割多级表头
|
||||
using (pen = new Pen(/*dgv.GridColor*/Color.FromArgb(229, 229, 229)))
|
||||
{
|
||||
//水平分隔线
|
||||
e.Graphics.DrawLine(pen, nLeft, nHeight, nLeft + nWidth, nHeight);
|
||||
//垂直分隔线
|
||||
var rtBounds = e.CellBounds;
|
||||
if (colNameCollection.IndexOf(name) == colNameCollection.Count - 1)
|
||||
e.Graphics.DrawLine(pen, rtBounds.Right - 1, rtBounds.Top + 1, rtBounds.Right - 1, rtBounds.Bottom - 2);
|
||||
else
|
||||
e.Graphics.DrawLine(pen, rtBounds.Right - 1, nHeight, rtBounds.Right - 1, rtBounds.Bottom - 2);
|
||||
}
|
||||
//绘制二级表头文字
|
||||
using (brush = new SolidBrush(e.CellStyle.ForeColor))
|
||||
{
|
||||
SizeF textSize = TextRenderer.MeasureText(dgv.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font);
|
||||
int nSubTop = nTop + nHeight;
|
||||
int nSubLeft = e.CellBounds.Left;
|
||||
int nSubWidth = e.CellBounds.Width;
|
||||
int nSubHeight = nHeight;
|
||||
PointF point = new PointF(nSubLeft + 6, nSubTop + (nSubHeight - textSize.Height) / 2);
|
||||
if (e.CellStyle.Alignment.ToString().Contains("Center"))
|
||||
point.X = nSubLeft + (nSubWidth - textSize.Width) / 2;
|
||||
else if (e.CellStyle.Alignment.ToString().Contains("Right"))
|
||||
point.X = nSubLeft + nSubWidth - textSize.Width - 6;
|
||||
if (e.CellStyle.Alignment.ToString().Contains("Top"))
|
||||
point.Y = nSubTop + 4;
|
||||
else if (e.CellStyle.Alignment.ToString().Contains("Bottom"))
|
||||
point.Y = nSubTop + nSubHeight - textSize.Height - 4;
|
||||
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
||||
e.Graphics.DrawString(dgv.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, brush, point);
|
||||
}
|
||||
}
|
||||
//绘制合并表头名称,只在最后绘制一次
|
||||
if (colNameCollection.IndexOf(name) == colNameCollection.Count - 1)
|
||||
{
|
||||
using (brush = new SolidBrush(e.CellStyle.ForeColor))
|
||||
{
|
||||
SizeF textSize = TextRenderer.MeasureText(headerText, e.CellStyle.Font);
|
||||
//单元列移出可见范围后将不再移动,需要强制改变绘制文字的位置
|
||||
if (nLeft < 0)
|
||||
nLeft = e.CellBounds.Right - nWidth;
|
||||
PointF point = new PointF(nLeft + 6, nTop + (nHeight - textSize.Height) / 2);
|
||||
if (e.CellStyle.Alignment.ToString().Contains("Center") || isMultiLevel)
|
||||
point.X = nLeft + (nWidth - textSize.Width) / 2;
|
||||
else if (e.CellStyle.Alignment.ToString().Contains("Right"))
|
||||
point.X = nLeft + nWidth - textSize.Width - 6;
|
||||
if (e.CellStyle.Alignment.ToString().Contains("Top"))
|
||||
point.Y = nTop + 4;
|
||||
else if (e.CellStyle.Alignment.ToString().Contains("Bottom"))
|
||||
point.Y = nTop + nHeight - textSize.Height - 4;
|
||||
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
||||
e.Graphics.DrawString(headerText, e.CellStyle.Font, brush, point);
|
||||
}
|
||||
}
|
||||
//e.set_Handled(true);
|
||||
e.Handled = true; // 告知系统不再处理表头单元格绘制事件
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user