官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
这个是在前面表格的基础上,扩展了自定义行实现的,当然也修改了一些列表控件以兼容
如果对前面的表格控件不了解,请移步查看
开始
实现树表格的思路就是,在行控件中再添加一个无标题的表格控件,当需要显示子节点的时候,将子节点数据加载到行里的表格控件中,然后处理一下事件,让事件可以穿透到最顶层就行了。
另外我们前面表格中的行个数是根据高度大小自动计算的,这里就会出现问题,当出现子节点表格的时候,就会导致重算个数和高度,所有我们在表格列表控件增加一个属性来禁用这个自动计算。
添加一个用户控件,命名UCDataGridViewTreeRow,实现接口IDataGridViewRow
属性
#region 属性
public event DataGridViewEventHandler CheckBoxChangeEvent; public event DataGridViewEventHandler CellClick; public event DataGridViewEventHandler SourceChanged; public List<DataGridViewColumnEntity> Columns
{
get;
set;
} public object DataSource
{
get;
set;
} public bool IsShowCheckBox
{
get;
set;
}
private bool m_isChecked;
public bool IsChecked
{
get
{
return m_isChecked;
} set
{
if (m_isChecked != value)
{
m_isChecked = value;
(this.panCells.Controls.Find("check", false)[] as UCCheckBox).Checked = value;
}
}
} int m_rowHeight = ;
public int RowHeight
{
get
{
return m_rowHeight;
}
set
{
m_rowHeight = value;
this.Height = value;
}
}
#endregion
构造函数处理一写东西,注意 this.ucDGVChild.ItemClick ,这个是将子节点表格的点击事件向上传递
public UCDataGridViewTreeRow()
{
InitializeComponent();
this.ucDGVChild.RowType = this.GetType();
this.ucDGVChild.IsAutoHeight = true;
this.SizeChanged += UCDataGridViewTreeRow_SizeChanged;
this.ucDGVChild.ItemClick += (a, b) =>
{
if (CellClick != null)
{
CellClick(a, new DataGridViewEventArgs()
{
CellControl = (a as Control),
CellIndex = (a as Control).Tag.ToInt()
});
}
}; }
实现接口函数BindingCellData,主要处理一下数据绑定后将子节点数据向下传递
public void BindingCellData()
{
for (int i = ; i < Columns.Count; i++)
{
DataGridViewColumnEntity com = Columns[i];
var cs = this.panCells.Controls.Find("lbl_" + com.DataField, false);
if (cs != null && cs.Length > )
{
var pro = DataSource.GetType().GetProperty(com.DataField);
if (pro != null)
{
var value = pro.GetValue(DataSource, null);
if (com.Format != null)
{
cs[].Text = com.Format(value);
}
else
{
cs[].Text = value.ToStringExt();
}
}
}
}
panLeft.Tag = ;
var proChildrens = DataSource.GetType().GetProperty("Childrens");
if (proChildrens != null)
{
var value = proChildrens.GetValue(DataSource, null);
if (value != null)
{
int intSourceCount = ;
if (value is DataTable)
{
intSourceCount = (value as DataTable).Rows.Count;
}
else if (typeof(IList).IsAssignableFrom(value.GetType()))
{
intSourceCount = (value as IList).Count;
}
if (intSourceCount > )
{
panLeft.BackgroundImage = Properties.Resources.caret_right;
panLeft.Enabled = true;
panChildGrid.Tag = value;
}
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
panChildGrid.Tag = null;
}
}
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
panChildGrid.Tag = null;
}
}
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
panChildGrid.Tag = null;
}
}
实现函数绑定列
public void ReloadCells()
{
try
{
ControlHelper.FreezeControl(this, true);
this.panCells.Controls.Clear();
this.panCells.ColumnStyles.Clear(); int intColumnsCount = Columns.Count();
if (Columns != null && intColumnsCount > )
{
if (IsShowCheckBox)
{
intColumnsCount++;
}
this.panCells.ColumnCount = intColumnsCount;
for (int i = ; i < intColumnsCount; i++)
{
Control c = null;
if (i == && IsShowCheckBox)
{
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F)); UCCheckBox box = new UCCheckBox();
box.Name = "check";
box.TextValue = "";
box.Size = new Size(, );
box.Dock = DockStyle.Fill;
box.CheckedChangeEvent += (a, b) =>
{
IsChecked = box.Checked;
this.ucDGVChild.Rows.ForEach(p => p.IsChecked = box.Checked);
if (CheckBoxChangeEvent != null)
{
CheckBoxChangeEvent(a, new DataGridViewEventArgs()
{
CellControl = box,
CellIndex =
});
}
};
c = box;
}
else
{
var item = Columns[i - (IsShowCheckBox ? : )];
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width)); Label lbl = new Label();
lbl.Tag = i - (IsShowCheckBox ? : );
lbl.Name = "lbl_" + item.DataField;
lbl.Font = new Font("微软雅黑", );
lbl.ForeColor = Color.Black;
lbl.AutoSize = false;
lbl.Dock = DockStyle.Fill;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.MouseDown += (a, b) =>
{
Item_MouseDown(a, b);
};
c = lbl;
}
this.panCells.Controls.Add(c, i, );
} }
}
finally
{
ControlHelper.FreezeControl(this, false);
}
}
当点击展开图标的时候处理子节点的数据绑定以及高度计算
private void panLeft_MouseDown(object sender, MouseEventArgs e)
{
try
{
ControlHelper.FreezeControl(this.FindForm(), true);
if (panLeft.Tag.ToInt() == )
{
var value = panChildGrid.Tag;
if (value != null)
{
panLeft.BackgroundImage = Properties.Resources.caret_down;
panLeft.Tag = ;
int intSourceCount = ;
if (value is DataTable)
{
intSourceCount = (value as DataTable).Rows.Count;
}
else if (typeof(IList).IsAssignableFrom(value.GetType()))
{
intSourceCount = (value as IList).Count;
}
this.panChildGrid.Height = RowHeight * intSourceCount;
if (panChildGrid.Height > )
{
if (value != this.ucDGVChild.DataSource)
{
this.ucDGVChild.Columns = Columns;
this.ucDGVChild.RowHeight = RowHeight;
this.ucDGVChild.HeadPadingLeft = this.panLeft.Width;
this.ucDGVChild.IsShowCheckBox = IsShowCheckBox;
this.ucDGVChild.DataSource = value;
this.ucDGVChild.Rows.ForEach(p => p.IsChecked =this.IsChecked);
}
}
} }
else
{
panLeft.Tag = ;
panChildGrid.Height = ;
panLeft.BackgroundImage = Properties.Resources.caret_right;
}
}
finally
{
ControlHelper.FreezeControl(this.FindForm(), false);
}
}
承载子节点表格的panel 在大小改变的时候,处理父级表格的大小。来防止出现多个滚动条
void UCDataGridViewTreeRow_SizeChanged(object sender, EventArgs e)
{
if (this.Parent.Parent.Parent != null && this.Parent.Parent.Parent.Name == "panChildGrid" && this.panLeft.Tag.ToInt() == )
{
int intHeight = this.Parent.Parent.Controls[].Controls.ToArray().Sum(p => p.Height);
if (this.Parent.Parent.Parent.Height != intHeight)
this.Parent.Parent.Parent.Height = intHeight;
}
}
完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; namespace HZH_Controls.Controls
{
[ToolboxItem(false)]
public partial class UCDataGridViewTreeRow : UserControl, IDataGridViewRow
{
#region 属性
public event DataGridViewEventHandler CheckBoxChangeEvent; public event DataGridViewEventHandler CellClick; public event DataGridViewEventHandler SourceChanged; public List<DataGridViewColumnEntity> Columns
{
get;
set;
} public object DataSource
{
get;
set;
} public bool IsShowCheckBox
{
get;
set;
}
private bool m_isChecked;
public bool IsChecked
{
get
{
return m_isChecked;
} set
{
if (m_isChecked != value)
{
m_isChecked = value;
(this.panCells.Controls.Find("check", false)[] as UCCheckBox).Checked = value;
}
}
} int m_rowHeight = ;
public int RowHeight
{
get
{
return m_rowHeight;
}
set
{
m_rowHeight = value;
this.Height = value;
}
}
#endregion public UCDataGridViewTreeRow()
{
InitializeComponent();
this.ucDGVChild.RowType = this.GetType();
this.ucDGVChild.IsAutoHeight = true;
this.SizeChanged += UCDataGridViewTreeRow_SizeChanged;
this.ucDGVChild.ItemClick += (a, b) =>
{
if (CellClick != null)
{
CellClick(a, new DataGridViewEventArgs()
{
CellControl = (a as Control),
CellIndex = (a as Control).Tag.ToInt()
});
}
}; } void UCDataGridViewTreeRow_SizeChanged(object sender, EventArgs e)
{
if (this.Parent.Parent.Parent != null && this.Parent.Parent.Parent.Name == "panChildGrid" && this.panLeft.Tag.ToInt() == )
{
int intHeight = this.Parent.Parent.Controls[].Controls.ToArray().Sum(p => p.Height);
if (this.Parent.Parent.Parent.Height != intHeight)
this.Parent.Parent.Parent.Height = intHeight;
}
} public void BindingCellData()
{
for (int i = ; i < Columns.Count; i++)
{
DataGridViewColumnEntity com = Columns[i];
var cs = this.panCells.Controls.Find("lbl_" + com.DataField, false);
if (cs != null && cs.Length > )
{
var pro = DataSource.GetType().GetProperty(com.DataField);
if (pro != null)
{
var value = pro.GetValue(DataSource, null);
if (com.Format != null)
{
cs[].Text = com.Format(value);
}
else
{
cs[].Text = value.ToStringExt();
}
}
}
}
panLeft.Tag = ;
var proChildrens = DataSource.GetType().GetProperty("Childrens");
if (proChildrens != null)
{
var value = proChildrens.GetValue(DataSource, null);
if (value != null)
{
int intSourceCount = ;
if (value is DataTable)
{
intSourceCount = (value as DataTable).Rows.Count;
}
else if (typeof(IList).IsAssignableFrom(value.GetType()))
{
intSourceCount = (value as IList).Count;
}
if (intSourceCount > )
{
panLeft.BackgroundImage = Properties.Resources.caret_right;
panLeft.Enabled = true;
panChildGrid.Tag = value;
}
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
panChildGrid.Tag = null;
}
}
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
panChildGrid.Tag = null;
}
}
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
panChildGrid.Tag = null;
}
} void Item_MouseDown(object sender, MouseEventArgs e)
{
if (CellClick != null)
{
CellClick(this, new DataGridViewEventArgs()
{
CellControl = this,
CellIndex = (sender as Control).Tag.ToInt()
});
}
} public void SetSelect(bool blnSelected)
{
if (blnSelected)
{
this.panMain.BackColor = Color.FromArgb(, , );
}
else
{
this.panMain.BackColor = Color.Transparent;
}
} public void ReloadCells()
{
try
{
ControlHelper.FreezeControl(this, true);
this.panCells.Controls.Clear();
this.panCells.ColumnStyles.Clear(); int intColumnsCount = Columns.Count();
if (Columns != null && intColumnsCount > )
{
if (IsShowCheckBox)
{
intColumnsCount++;
}
this.panCells.ColumnCount = intColumnsCount;
for (int i = ; i < intColumnsCount; i++)
{
Control c = null;
if (i == && IsShowCheckBox)
{
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F)); UCCheckBox box = new UCCheckBox();
box.Name = "check";
box.TextValue = "";
box.Size = new Size(, );
box.Dock = DockStyle.Fill;
box.CheckedChangeEvent += (a, b) =>
{
IsChecked = box.Checked;
this.ucDGVChild.Rows.ForEach(p => p.IsChecked = box.Checked);
if (CheckBoxChangeEvent != null)
{
CheckBoxChangeEvent(a, new DataGridViewEventArgs()
{
CellControl = box,
CellIndex =
});
}
};
c = box;
}
else
{
var item = Columns[i - (IsShowCheckBox ? : )];
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width)); Label lbl = new Label();
lbl.Tag = i - (IsShowCheckBox ? : );
lbl.Name = "lbl_" + item.DataField;
lbl.Font = new Font("微软雅黑", );
lbl.ForeColor = Color.Black;
lbl.AutoSize = false;
lbl.Dock = DockStyle.Fill;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.MouseDown += (a, b) =>
{
Item_MouseDown(a, b);
};
c = lbl;
}
this.panCells.Controls.Add(c, i, );
} }
}
finally
{
ControlHelper.FreezeControl(this, false);
}
} private void panChildGrid_SizeChanged(object sender, EventArgs e)
{
int intHeight = RowHeight + panChildGrid.Height;
if (panChildGrid.Height != )
this.ucDGVChild.Height = panChildGrid.Height;
if (this.Height != intHeight)
this.Height = intHeight;
} private void panLeft_MouseDown(object sender, MouseEventArgs e)
{
try
{
ControlHelper.FreezeControl(this.FindForm(), true);
if (panLeft.Tag.ToInt() == )
{
var value = panChildGrid.Tag;
if (value != null)
{
panLeft.BackgroundImage = Properties.Resources.caret_down;
panLeft.Tag = ;
int intSourceCount = ;
if (value is DataTable)
{
intSourceCount = (value as DataTable).Rows.Count;
}
else if (typeof(IList).IsAssignableFrom(value.GetType()))
{
intSourceCount = (value as IList).Count;
}
this.panChildGrid.Height = RowHeight * intSourceCount;
if (panChildGrid.Height > )
{
if (value != this.ucDGVChild.DataSource)
{
this.ucDGVChild.Columns = Columns;
this.ucDGVChild.RowHeight = RowHeight;
this.ucDGVChild.HeadPadingLeft = this.panLeft.Width;
this.ucDGVChild.IsShowCheckBox = IsShowCheckBox;
this.ucDGVChild.DataSource = value;
this.ucDGVChild.Rows.ForEach(p => p.IsChecked =this.IsChecked);
}
}
} }
else
{
panLeft.Tag = ;
panChildGrid.Height = ;
panLeft.BackgroundImage = Properties.Resources.caret_right;
}
}
finally
{
ControlHelper.FreezeControl(this.FindForm(), false);
}
} private void ucDGVChild_SizeChanged(object sender, EventArgs e)
{ }
}
}
namespace HZH_Controls.Controls
{
partial class UCDataGridViewTreeRow
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panCells = new System.Windows.Forms.TableLayoutPanel();
this.panLeft = new System.Windows.Forms.Panel();
this.panChildGrid = new System.Windows.Forms.Panel();
this.panChildLeft = new System.Windows.Forms.Panel();
this.panMain = new System.Windows.Forms.Panel();
this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
this.ucDGVChild = new HZH_Controls.Controls.UCDataGridView();
this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
this.panChildGrid.SuspendLayout();
this.panMain.SuspendLayout();
this.SuspendLayout();
//
// panCells
//
this.panCells.ColumnCount = ;
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.panCells.Dock = System.Windows.Forms.DockStyle.Fill;
this.panCells.Location = new System.Drawing.Point(, );
this.panCells.Name = "panCells";
this.panCells.RowCount = ;
this.panCells.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.panCells.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 55F));
this.panCells.Size = new System.Drawing.Size(, );
this.panCells.TabIndex = ;
//
// panLeft
//
this.panLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.panLeft.Dock = System.Windows.Forms.DockStyle.Left;
this.panLeft.Location = new System.Drawing.Point(, );
this.panLeft.Name = "panLeft";
this.panLeft.Size = new System.Drawing.Size(, );
this.panLeft.TabIndex = ;
this.panLeft.Tag = "";
this.panLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panLeft_MouseDown);
//
// panChildGrid
//
this.panChildGrid.Controls.Add(this.ucDGVChild);
this.panChildGrid.Controls.Add(this.ucSplitLine_V1);
this.panChildGrid.Controls.Add(this.panChildLeft);
this.panChildGrid.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panChildGrid.Location = new System.Drawing.Point(, );
this.panChildGrid.Name = "panChildGrid";
this.panChildGrid.Size = new System.Drawing.Size(, );
this.panChildGrid.TabIndex = ;
this.panChildGrid.SizeChanged += new System.EventHandler(this.panChildGrid_SizeChanged);
//
// panChildLeft
//
this.panChildLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.panChildLeft.Dock = System.Windows.Forms.DockStyle.Left;
this.panChildLeft.Location = new System.Drawing.Point(, );
this.panChildLeft.Name = "panChildLeft";
this.panChildLeft.Size = new System.Drawing.Size(, );
this.panChildLeft.TabIndex = ;
this.panChildLeft.Tag = "";
//
// panMain
//
this.panMain.Controls.Add(this.panCells);
this.panMain.Controls.Add(this.panLeft);
this.panMain.Dock = System.Windows.Forms.DockStyle.Fill;
this.panMain.Location = new System.Drawing.Point(, );
this.panMain.Name = "panMain";
this.panMain.Size = new System.Drawing.Size(, );
this.panMain.TabIndex = ;
//
// ucSplitLine_H1
//
this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ucSplitLine_H1.Location = new System.Drawing.Point(, );
this.ucSplitLine_H1.Name = "ucSplitLine_H1";
this.ucSplitLine_H1.Size = new System.Drawing.Size(, );
this.ucSplitLine_H1.TabIndex = ;
this.ucSplitLine_H1.TabStop = false;
//
// ucDGVChild
//
this.ucDGVChild.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ucDGVChild.BackColor = System.Drawing.Color.White;
this.ucDGVChild.HeadFont = new System.Drawing.Font("微软雅黑", 12F);
this.ucDGVChild.HeadHeight = ;
this.ucDGVChild.HeadPadingLeft = ;
this.ucDGVChild.HeadTextColor = System.Drawing.Color.Black;
this.ucDGVChild.IsAutoHeight = false;
this.ucDGVChild.IsShowCheckBox = false;
this.ucDGVChild.IsShowHead = false;
this.ucDGVChild.Location = new System.Drawing.Point(, );
this.ucDGVChild.Name = "ucDGVChild";
this.ucDGVChild.Page = null;
this.ucDGVChild.RowHeight = ;
this.ucDGVChild.RowType = typeof(HZH_Controls.Controls.UCDataGridViewRow);
this.ucDGVChild.Size = new System.Drawing.Size(, );
this.ucDGVChild.TabIndex = ;
this.ucDGVChild.SizeChanged += new System.EventHandler(this.ucDGVChild_SizeChanged);
//
// ucSplitLine_V1
//
this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
this.ucSplitLine_V1.Location = new System.Drawing.Point(, );
this.ucSplitLine_V1.Name = "ucSplitLine_V1";
this.ucSplitLine_V1.Size = new System.Drawing.Size(, );
this.ucSplitLine_V1.TabIndex = ;
this.ucSplitLine_V1.TabStop = false;
//
// UCDataGridViewTreeRow
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.panMain);
this.Controls.Add(this.ucSplitLine_H1);
this.Controls.Add(this.panChildGrid);
this.Name = "UCDataGridViewTreeRow";
this.Size = new System.Drawing.Size(, );
this.panChildGrid.ResumeLayout(false);
this.panMain.ResumeLayout(false);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.TableLayoutPanel panCells;
private UCSplitLine_H ucSplitLine_H1;
private System.Windows.Forms.Panel panLeft;
private System.Windows.Forms.Panel panChildGrid;
private UCDataGridView ucDGVChild;
private System.Windows.Forms.Panel panChildLeft;
private System.Windows.Forms.Panel panMain;
private UCSplitLine_V ucSplitLine_V1;
}
}
我这里写死了,如果要使用树表格的话,子数据就要用属性Childrens,比如下面的实体就是了
public class TestModel
{
public string ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime Birthday { get; set; }
public int Sex { get; set; }
public List<TestModel> Childrens { get; set; }
}
使用实例如下
this.ucDataGridView1.RowType = typeof(UCDataGridViewTreeRow);
this.ucDataGridView1.IsAutoHeight = true; List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = , WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = , WidthType = SizeType.Percent });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年龄", Width = , WidthType = SizeType.Percent });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = , WidthType = SizeType.Percent, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sex", HeadText = "性别", Width = , WidthType = SizeType.Percent, Format = (a) => { return ((int)a) == ? "女" : "男"; } });
this.ucDataGridView1.Columns = lstCulumns;
this.ucDataGridView1.IsShowCheckBox = true;
List<object> lstSource = new List<object>();
for (int i = ; i < ; i++)
{
TestModel model = new TestModel()
{
ID = i.ToString(),
Age = * i,
Name = "姓名——" + i,
Birthday = DateTime.Now.AddYears(-),
Sex = i %
};
lstSource.Add(model);
AddChilds(model, );
} var page = new UCPagerControl2();
page.DataSource = lstSource;
this.ucDataGridView1.Page = page;
this.ucDataGridView1.First(); private void AddChilds(TestModel tm, int intCount)
{
if (intCount <= )
return;
tm.Childrens = new List<TestModel>();
for (int i = ; i < ; i++)
{
TestModel model = new TestModel()
{
ID = i.ToString(),
Age = * i,
Name = intCount + "——" + i,
Birthday = DateTime.Now.AddYears(-),
Sex = i %
};
tm.Childrens.Add(model);
AddChilds(model, intCount - );
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧