一 重写表格
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace DataGrideViewControl
{
public class DataGridViewGroupCell : DataGridViewTextBoxCell
{
#region Variant
/// <summary>
/// 标示的宽度
/// </summary>
const int PLUS_WIDTH = 24;
/// <summary>
/// 标示的区域
/// </summary>
Rectangle groupPlusRect;
#endregion
#region Init
public DataGridViewGroupCell()
{
groupLevel = 1;
}
#endregion
#region Property
int groupLevel;
/// <summary>
/// 组级别(以1开始)
/// </summary>
public int GroupLevel
{
get {
return groupLevel; }
set {
groupLevel = value; }
}
DataGridViewGroupCell parentCell;
/// <summary>
/// 父节点
/// </summary>
public DataGridViewGroupCell ParentCell
{
get
{
return parentCell;
}
set
{
if (value == null)
throw new NullReferenceException("父节点不可为空");
if (!(value is DataGridViewGroupCell))
throw new ArgumentException("父节点必须为 DataGridViewGroupCell 类型");
parentCell = value;
parentCell.AddChildCell(this);
}
}
private bool collapsed;
/// <summary>
/// 是否收起
/// </summary>
public bool Collapsed
{
get {
return collapsed; }
}
private List<DataGridViewGroupCell> childCells = null;
/// <summary>
/// 所有的子结点
/// </summary>
public DataGridViewGroupCell[] ChildCells
{
get
{
if (childCells == null)
return null;
return childCells.ToArray();
}
}
/// <summary>
/// 取得组标示(有+或-号的框)的区域
/// </summary>
public Rectangle GroupPlusRect
{
get
{
return groupPlusRect;
}
}
bool bPaint = true;
/// <summary>
/// 是否重绘
/// </summary>
public bool BPaint
{
get {
return bPaint; }
set {
bPaint = value; }
}
#endregion
#region 添加子节点
/// <summary>
/// 添加子结点
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
public int AddChildCell(DataGridViewGroupCell cell)
{
return AddChildCellRange(new DataGridViewGroupCell[] {
cell });
}
public int AddChildCellRange(DataGridViewGroupCell[] cells)
{
bool needRedraw = false;
if (childCells == null)
{
//需要画一个加号
childCells = new List<DataGridViewGroupCell>();
needRedraw = true;
}
foreach (DataGridViewGroupCell cell in cells)

本文介绍如何使用 C# 在 DataGridView 控件中实现自定义分组功能,包括创建自定义 DataGridViewGroupCell 类来实现组节点的绘制和交互,以及通过实例演示如何添加和展示分组数据。
2014

被折叠的 条评论
为什么被折叠?



