Commit 09e1bd73 kwwwvagaa

优化树表格显示

1 个父辈 6b547591
......@@ -507,6 +507,8 @@ namespace HZH_Controls.Controls
#endregion
#region 公共函数
/// <summary>
/// 刷新数据
......@@ -779,7 +781,6 @@ namespace HZH_Controls.Controls
private void UCDataGridView_Paint(object sender, PaintEventArgs e)
{
}
}
}
}
......@@ -50,13 +50,9 @@ namespace HZH_Controls.Controls
{
this.panCells = new System.Windows.Forms.TableLayoutPanel();
this.panLeft = new System.Windows.Forms.Panel();
this.panChildGrid = new System.Windows.Forms.Panel();
this.ucDGVChild = new HZH_Controls.Controls.UCDataGridView();
this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
this.panChildLeft = new System.Windows.Forms.Panel();
this.panMain = new System.Windows.Forms.Panel();
this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
this.panChildGrid.SuspendLayout();
this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
this.panMain.SuspendLayout();
this.SuspendLayout();
//
......@@ -71,7 +67,7 @@ namespace HZH_Controls.Controls
this.panCells.RowCount = 1;
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(637, 64);
this.panCells.Size = new System.Drawing.Size(636, 64);
this.panCells.TabIndex = 2;
//
// panLeft
......@@ -85,89 +81,46 @@ namespace HZH_Controls.Controls
this.panLeft.Tag = "0";
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(0, 65);
this.panChildGrid.Name = "panChildGrid";
this.panChildGrid.Size = new System.Drawing.Size(661, 0);
this.panChildGrid.TabIndex = 0;
this.panChildGrid.SizeChanged += new System.EventHandler(this.panChildGrid_SizeChanged);
//
// 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.Columns = null;
this.ucDGVChild.DataSource = null;
this.ucDGVChild.HeadFont = new System.Drawing.Font("微软雅黑", 12F);
this.ucDGVChild.HeadHeight = 40;
this.ucDGVChild.HeadPadingLeft = 0;
this.ucDGVChild.HeadTextColor = System.Drawing.Color.Black;
this.ucDGVChild.IsShowCheckBox = false;
this.ucDGVChild.IsShowHead = false;
this.ucDGVChild.Location = new System.Drawing.Point(25, 0);
this.ucDGVChild.Name = "ucDGVChild";
this.ucDGVChild.RowHeight = 40;
this.ucDGVChild.RowType = typeof(HZH_Controls.Controls.UCDataGridViewRow);
this.ucDGVChild.Size = new System.Drawing.Size(636, 100);
this.ucDGVChild.TabIndex = 0;
this.ucDGVChild.SizeChanged += new System.EventHandler(this.ucDGVChild_SizeChanged);
//
// ucSplitLine_V1
//
this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
this.ucSplitLine_V1.Location = new System.Drawing.Point(24, 0);
this.ucSplitLine_V1.Name = "ucSplitLine_V1";
this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 0);
this.ucSplitLine_V1.TabIndex = 0;
this.ucSplitLine_V1.TabStop = false;
//
// panChildLeft
//
this.panChildLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.panChildLeft.Dock = System.Windows.Forms.DockStyle.Left;
this.panChildLeft.Location = new System.Drawing.Point(0, 0);
this.panChildLeft.Name = "panChildLeft";
this.panChildLeft.Size = new System.Drawing.Size(24, 0);
this.panChildLeft.TabIndex = 1;
this.panChildLeft.Tag = "0";
//
// 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(0, 0);
this.panMain.Location = new System.Drawing.Point(1, 0);
this.panMain.Name = "panMain";
this.panMain.Size = new System.Drawing.Size(661, 64);
this.panMain.Size = new System.Drawing.Size(660, 64);
this.panMain.TabIndex = 0;
//
// ucSplitLine_H1
//
this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 64);
this.ucSplitLine_H1.Location = new System.Drawing.Point(1, 64);
this.ucSplitLine_H1.Name = "ucSplitLine_H1";
this.ucSplitLine_H1.Size = new System.Drawing.Size(661, 1);
this.ucSplitLine_H1.Size = new System.Drawing.Size(660, 1);
this.ucSplitLine_H1.TabIndex = 1;
this.ucSplitLine_H1.TabStop = false;
//
// ucSplitLine_V1
//
this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
this.ucSplitLine_V1.Location = new System.Drawing.Point(0, 0);
this.ucSplitLine_V1.Name = "ucSplitLine_V1";
this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 65);
this.ucSplitLine_V1.TabIndex = 0;
this.ucSplitLine_V1.TabStop = false;
this.ucSplitLine_V1.Visible = 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.Controls.Add(this.ucSplitLine_V1);
this.Name = "UCDataGridViewTreeRow";
this.Size = new System.Drawing.Size(661, 65);
this.panChildGrid.ResumeLayout(false);
this.panMain.ResumeLayout(false);
this.ResumeLayout(false);
......@@ -188,24 +141,9 @@ namespace HZH_Controls.Controls
/// </summary>
private System.Windows.Forms.Panel panLeft;
/// <summary>
/// The pan child grid
/// </summary>
private System.Windows.Forms.Panel panChildGrid;
/// <summary>
/// The uc DGV child
/// </summary>
private UCDataGridView ucDGVChild;
/// <summary>
/// The pan child left
/// </summary>
private System.Windows.Forms.Panel panChildLeft;
/// <summary>
/// The pan main
/// </summary>
private System.Windows.Forms.Panel panMain;
/// <summary>
/// The uc split line v1
/// </summary>
private UCSplitLine_V ucSplitLine_V1;
}
}
......@@ -74,6 +74,21 @@ namespace HZH_Controls.Controls
set;
}
private int rowLevel = 0;
/// <summary>
/// 折叠的第几层,用于缩进
/// </summary>
public int RowLevel
{
get { return rowLevel; }
set
{
rowLevel = value;
this.Padding = new Padding(this.panLeft.Width / 2 * value, this.Padding.Top, this.panLeft.Right, this.Padding.Bottom);
this.ucSplitLine_V1.Visible = value > 0;
}
}
/// <summary>
/// 行号,树状图目前没有给予行号
/// </summary>
......@@ -134,53 +149,55 @@ namespace HZH_Controls.Controls
this.Height = value;
}
}
#endregion
private List<UCDataGridViewTreeRow> childrenRows = new List<UCDataGridViewTreeRow>();
public List<UCDataGridViewTreeRow> ChildrenRows
{
get { return childrenRows; }
set { childrenRows = value; }
}
private bool? isOpened = false;
/// <summary>
/// Initializes a new instance of the <see cref="UCDataGridViewTreeRow" /> class.
/// 是否打开状态
/// </summary>
public UCDataGridViewTreeRow()
public bool? IsOpened
{
InitializeComponent();
this.ucDGVChild.RowType = this.GetType();
this.SizeChanged += UCDataGridViewTreeRow_SizeChanged;
this.ucDGVChild.ItemClick += (a, b) =>
get { return isOpened; }
set
{
if (CellClick != null)
isOpened = value;
if (value.HasValue)
{
CellClick(a, new DataGridViewEventArgs()
panLeft.Enabled = true;
if (value.Value)
{
panLeft.BackgroundImage = Properties.Resources.caret_down;
}
else
{
CellControl = (a as Control),
CellIndex = (a as Control).Tag.ToInt()
});
panLeft.BackgroundImage = Properties.Resources.caret_right;
}
}
};
this.ucDGVChild.AutoScroll = false;
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
}
}
}
#endregion
/// <summary>
/// Handles the SizeChanged event of the UCDataGridViewTreeRow control.
/// Initializes a new instance of the <see cref="UCDataGridViewTreeRow" /> class.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void UCDataGridViewTreeRow_SizeChanged(object sender, EventArgs e)
public UCDataGridViewTreeRow()
{
if (this.Parent != null)
{
if (this.Parent.Parent.Parent != null && this.Parent.Parent.Parent.Name == "panChildGrid" && this.panLeft.Tag.ToInt() == 1)
{
int intHeight = this.Parent.Parent.Controls[0].Controls.ToArray().Sum(p => p.Height);
if (this.Parent.Parent.Parent.Height != intHeight)
this.Parent.Parent.Parent.Height = intHeight;
}
}
InitializeComponent();
}
/// <summary>
/// 绑定数据到Cell
/// </summary>
/// <returns>返回true则表示已处理过,否则将进行默认绑定(通常只针对有Text值的控件)</returns>
public void BindingCellData()
{
for (int i = 0; i < Columns.Count; i++)
......@@ -212,47 +229,22 @@ namespace HZH_Controls.Controls
cell.SetBindSource(DataSource);
}
}
panLeft.Tag = 0;
IsOpened = false;
var proChildrens = DataSource.GetType().GetProperty("Childrens");
if (proChildrens != null)
{
var value = proChildrens.GetValue(DataSource, null);
if (value != null)
{
int intSourceCount = 0;
if (value is DataTable)
{
intSourceCount = (value as DataTable).Rows.Count;
}
else if (typeof(IList).IsAssignableFrom(value.GetType()))
{
intSourceCount = (value as IList).Count;
}
if (intSourceCount > 0)
{
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;
IsOpened = null;
}
}
else
{
panLeft.BackgroundImage = null;
panLeft.Enabled = false;
panChildGrid.Tag = null;
IsOpened = null;
}
}
......@@ -308,6 +300,7 @@ namespace HZH_Controls.Controls
intColumnsCount++;
}
this.panCells.ColumnCount = intColumnsCount;
bool blnFirst = true;
for (int i = 0; i < intColumnsCount; i++)
{
Control c = null;
......@@ -320,29 +313,16 @@ namespace HZH_Controls.Controls
box.TextValue = "";
box.Size = new Size(30, 30);
box.Dock = DockStyle.Fill;
box.CheckedChangeEvent += (a, b) =>
{
IsChecked = box.Checked;
if (this.ucDGVChild.Rows != null)
{
this.ucDGVChild.Rows.ForEach(p => p.IsChecked = box.Checked);
if (CheckBoxChangeEvent != null)
{
CheckBoxChangeEvent(a, new DataGridViewEventArgs()
{
CellControl = box,
CellIndex = 0
});
}
}
};
c = box;
}
else
{
var item = Columns[i - (IsShowCheckBox ? 1 : 0)];
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width));
var w = item.Width - (blnFirst ? (this.panLeft.Width / 2 * rowLevel) : 0);
if (w < 5)
w = 5;
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, w));
blnFirst = false;
if (item.CustomCellType == null)
{
Label lbl = new Label();
......@@ -359,9 +339,9 @@ namespace HZH_Controls.Controls
};
c = lbl;
}
else
else
{
Control cc = (Control)Activator.CreateInstance(item.CustomCellType);
Control cc = (Control)Activator.CreateInstance(item.CustomCellType);
cc.Dock = DockStyle.Fill;
c = cc;
}
......@@ -377,21 +357,7 @@ namespace HZH_Controls.Controls
}
}
/// <summary>
/// Handles the SizeChanged event of the panChildGrid control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private void panChildGrid_SizeChanged(object sender, EventArgs e)
{
int intHeight = RowHeight + panChildGrid.Height;
if (panChildGrid.Height != 0)
this.ucDGVChild.Height = panChildGrid.Height;
if (this.Height != intHeight)
{
this.Height = intHeight;
}
}
/// <summary>
/// Handles the MouseDown event of the panLeft control.
......@@ -402,57 +368,74 @@ namespace HZH_Controls.Controls
{
try
{
if (!IsOpened.HasValue)
return;
ControlHelper.FreezeControl(this.FindForm(), true);
if (panLeft.Tag.ToInt() == 0)
if (!IsOpened.Value)
{
var value = panChildGrid.Tag;
if (value != null)
IsOpened = !IsOpened;
if (childrenRows.Count > 0)
{
panLeft.BackgroundImage = Properties.Resources.caret_down;
panLeft.Tag = 1;
int intSourceCount = 0;
if (value is DataTable)
{
intSourceCount = (value as DataTable).Rows.Count;
}
else if (typeof(IList).IsAssignableFrom(value.GetType()))
childrenRows.ForEach(p =>
{
intSourceCount = (value as IList).Count;
}
this.panChildGrid.Height = RowHeight * intSourceCount;
if (panChildGrid.Height > 0)
p.IsChecked = IsChecked;
p.Visible = true;
});
}
else
{
var proChildrens = DataSource.GetType().GetProperty("Childrens");
if (proChildrens != null)
{
if (value != this.ucDGVChild.DataSource)
var value = proChildrens.GetValue(DataSource, null);
int intSourceCount = 0;
if (value is DataTable)
{
intSourceCount = (value as DataTable).Rows.Count;
}
else if (typeof(IList).IsAssignableFrom(value.GetType()))
{
intSourceCount = (value as IList).Count;
}
for (int i = intSourceCount - 1; i >= 0; i--)
{
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);
UCDataGridViewTreeRow row = new UCDataGridViewTreeRow();
if (value is DataTable)
{
row.DataSource = (value as DataTable).Rows[i];
}
else
{
row.DataSource = (value as IList)[i];
}
row.RowLevel = RowLevel + 1;
row.Columns = Columns;
row.IsShowCheckBox = IsShowCheckBox;
row.ReloadCells();
row.BindingCellData();
Control rowControl = (row as Control);
row.RowHeight = m_rowHeight;
rowControl.Dock = DockStyle.Top;
row.CellClick += (a, b) => { CellClick(rowControl, b); };
row.CheckBoxChangeEvent += (a, b) => { CheckBoxChangeEvent(rowControl, b); };
row.RowCustomEvent += (a, b) => { if (RowCustomEvent != null) { RowCustomEvent(a, b); } };
row.SourceChanged += SourceChanged;
ChildrenRows.Add(row);
row.RowIndex = ChildrenRows.IndexOf(row);
this.Parent.Controls.Add(rowControl);
var index = this.Parent.Controls.GetChildIndex(this);
this.Parent.Controls.SetChildIndex(row, index);
}
}
}
}
else
{
Control[] cs = panChildGrid.Controls.Find("panLeft", true);
foreach (var item in cs)
{
item.Tag = 0;
item.BackgroundImage = Properties.Resources.caret_right;
}
Control[] cs1 = panChildGrid.Controls.Find("panChildGrid", true);
foreach (var item in cs1)
{
item.Height = 0;
}
panChildGrid.Height = 0;
panLeft.BackgroundImage = Properties.Resources.caret_right;
HideChildrenRows(this);
this.Height = m_rowHeight;
panLeft.Tag = 0;
}
}
finally
......@@ -461,14 +444,17 @@ namespace HZH_Controls.Controls
}
}
/// <summary>
/// Handles the SizeChanged event of the ucDGVChild control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private void ucDGVChild_SizeChanged(object sender, EventArgs e)
private void HideChildrenRows(UCDataGridViewTreeRow row)
{
if (row.ChildrenRows.Count > 0)
{
foreach (var item in row.ChildrenRows)
{
HideChildrenRows(item);
item.Hide();
}
row.IsOpened = false;
}
}
}
}
......@@ -37,6 +37,7 @@
this.ucDataGridView1.BackColor = System.Drawing.Color.White;
this.ucDataGridView1.Columns = null;
this.ucDataGridView1.DataSource = null;
this.ucDataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.ucDataGridView1.HeadFont = new System.Drawing.Font("微软雅黑", 12F);
this.ucDataGridView1.HeadHeight = 40;
this.ucDataGridView1.HeadPadingLeft = 0;
......@@ -47,13 +48,13 @@
this.ucDataGridView1.Name = "ucDataGridView1";
this.ucDataGridView1.RowHeight = 41;
this.ucDataGridView1.RowType = typeof(HZH_Controls.Controls.UCDataGridViewRow);
this.ucDataGridView1.Size = new System.Drawing.Size(722, 583);
this.ucDataGridView1.Size = new System.Drawing.Size(739, 599);
this.ucDataGridView1.TabIndex = 4;
//
// UCTestGridTable
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Black;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.ucDataGridView1);
this.Name = "UCTestGridTable";
this.Size = new System.Drawing.Size(739, 599);
......
......@@ -37,6 +37,7 @@
this.ucDataGridView1.BackColor = System.Drawing.Color.White;
this.ucDataGridView1.Columns = null;
this.ucDataGridView1.DataSource = null;
this.ucDataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.ucDataGridView1.HeadFont = new System.Drawing.Font("微软雅黑", 12F);
this.ucDataGridView1.HeadHeight = 40;
this.ucDataGridView1.HeadPadingLeft = 24;
......@@ -47,16 +48,16 @@
this.ucDataGridView1.Name = "ucDataGridView1";
this.ucDataGridView1.RowHeight = 41;
this.ucDataGridView1.RowType = typeof(HZH_Controls.Controls.UCDataGridViewRow);
this.ucDataGridView1.Size = new System.Drawing.Size(807, 655);
this.ucDataGridView1.Size = new System.Drawing.Size(817, 674);
this.ucDataGridView1.TabIndex = 5;
//
// UCTestTreeGridTable
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Black;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.ucDataGridView1);
this.Name = "UCTestTreeGridTable";
this.Size = new System.Drawing.Size(825, 674);
this.Size = new System.Drawing.Size(817, 674);
this.Load += new System.EventHandler(this.UCTestTreeGridTable_Load);
this.ResumeLayout(false);
......
......@@ -23,7 +23,7 @@ namespace Test.UC
{
this.ucDataGridView1.RowType = typeof(UCDataGridViewTreeRow);
List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = 70, WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = 100, WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = 150, WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年龄", Width = 150, WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = 150, WidthType = SizeType.Absolute, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } });
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!