winform中可以折叠的datagridview,自定义控件。
目前实现了两层折叠,可以进一步拓展。
public partial class CollDataGridView : DataGridView
{
public CollDataGridView()
{
InitializeComponent();
}
public CollDataGridView(IContainer container)
{
container.Add(this);
InitializeComponent();
this.RowHeadersVisible = true;
this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
this.ImageWidth = 15;
this.ImageHeight = 15;
this.RowHeadersDefaultCellStyle.Padding = new Padding(this.RowHeadersWidth);
this.CurrentCellDirtyStateChanged += CollDataGridView_CurrentCellDirtyStateChanged;
}
private void CollDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.IsCurrentCellDirty)
{
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
#region property
/// <summary>
/// 行首图片宽度
/// </summary>
[
Category("CollDataGridViewProperties"),
Description("行首图片宽度"),
Bindable(true)
]
public int ImageWidth { get; set; }
/// <summary>
/// 行首图片高度
/// </summary>
[
Category("CollDataGridViewProperties"),
Description("行首图片高度"),
Bindable(true)
]
public int ImageHeight { get; set; }
/// <summary>
/// 行首收缩图标
/// </summary>
[
Category("CollDataGridViewProperties"),
Description("行首收缩图标"),
Bindable(true)
]
public Image ImgExpand { get; set; }
/// <summary>
/// 行首展开图片
/// </summary>
[
Category("CollDataGridViewProperties"),
Description("行首展开图片"),
Bindable(true)
]
public Image ImgCollapse { get; set; }
#endregion
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
base.OnRowPostPaint(e);
DataGridViewRow row = this.Rows[e.RowIndex];
Rectangle rectBack = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.RowHeadersWidth, e.RowBounds.Height - 1);
Rectangle rectLineBottom = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y + e.RowBounds.Height - 1, this.RowHeadersWidth, 1);
if (row