WinForm中DataGridView折叠控件【超好看】

刚到一家新公司,领导下发任务要用cs系统做一个表格折叠显示,这真是把我难倒了,自己工作6年一直以来都是做BS的系统。这如果在BS里面那太简单了,JqGrid默认都自带,可是DataGridview不支持折叠啊。自己一点经验没有,怎么办呢?于是上网搜了相关视频,资料,开始学习起来。最后借鉴源码封了这么一个东西,发出来分享下,也能让自己加深印象。

首先不多说,上图。如果大家感谢还不错,请继续往下阅读:

大概的效果就是这样。

上代码。

1、首先重写DataGridview,代码如下:

public class MasterControl : DataGridView
{
#region 字段
private List<int> rowCurrent = new List<int>();
internal static int rowDefaultHeight = ;
internal static int rowExpandedHeight = ;
internal static int rowDefaultDivider = ;
internal static int rowExpandedDivider = - ;
internal static int rowDividerMargin = ;
internal static bool collapseRow;
     //detailControl变量作为一个容器用来保存子表格
public detailControl childView = new detailControl() { Visible = false }; // VBConversions Note: Initial value cannot be assigned here since it is non-static. Assignment has been moved to the class constructors.
//
internal System.Windows.Forms.ImageList RowHeaderIconList;
private System.ComponentModel.Container components = null;
//
DataSet _cDataset;
string _foreignKey;
string _primaryKey;
string _filterFormat;
private controlType EControlType;
public int ExpandRowIndex = ;
#endregion
#region 构造函数
/// <summary>
/// 通过传递过来的枚举判断是两级还是三级展开,表的对应关系通过Relations来读取
/// 所以调用此构造函数的时候必须要讲Relations设置正确,才能正确显示层级关系。
/// oDataSet.Relations.Add("", oDataSet.Tables["T"].Columns["Menu_ID"], oDataSet.Tables["T"].Columns["Menu_ID"]);
/// oDataSet.Relations.Add("", oDataSet.Tables["T"].Columns["Menu_Name"], oDataSet.Tables["T"].Columns["Menu_Name"]);
/// 这两次Add的顺序不能颠倒,必须先添加一、二级的表关联,再添加二、三级的表关联
/// </summary>
/// <param name="cDataset">数据源DataSet,里面还有各个表的对应关系</param>
/// <param name="eControlType">枚举类型</param>
public MasterControl(DataSet cDataset, controlType eControlType)
{
SetMasterControl(cDataset, eControlType);
}
/// <summary>
/// 第二种使用方法
/// </summary>
/// <param name="lstData">折叠控件第一层的集合</param>
/// <param name="lstData">折叠控件第二层的集合</param>
/// <param name="lstData">折叠控件第三层的集合</param>
/// <param name="dicRelateKey">第一二层之间对应主外键</param>
/// <param name="dicRelateKey">第二三层之间对应主外键</param>
/// <param name="eControlType">枚举类型</param>
public MasterControl(object lstData, object lstData,
object lstData, Dictionary<string, string> dicRelateKey,
Dictionary<string ,string>dicRelateKey, controlType eControlType)
{
var oDataSet = new DataSet();
try
{
var oTable = new DataTable();
oTable = Fill(lstData);
oTable.TableName = "T";
var oTable = Fill(lstData);
oTable.TableName = "T";
if (lstData == null || dicRelateKey == null || dicRelateKey.Keys.Count <= )
{
oDataSet.Tables.AddRange(new DataTable[] { oTable, oTable });
oDataSet.Relations.Add("", oDataSet.Tables["T"].Columns[dicRelateKey.Keys.FirstOrDefault()], oDataSet.Tables["T"].Columns[dicRelateKey.Values.FirstOrDefault()]);
}
else
{
var oTable = Fill(lstData);
oTable.TableName = "T";
oDataSet.Tables.AddRange(new DataTable[] { oTable, oTable, oTable });
//这是对应关系的时候主键必须唯一
oDataSet.Relations.Add("", oDataSet.Tables["T"].Columns[dicRelateKey.Keys.FirstOrDefault()], oDataSet.Tables["T"].Columns[dicRelateKey.Values.FirstOrDefault()]);
oDataSet.Relations.Add("", oDataSet.Tables["T"].Columns[dicRelateKey.Keys.FirstOrDefault()], oDataSet.Tables["T"].Columns[dicRelateKey.Values.FirstOrDefault()]);
}
}
catch
{
oDataSet = new DataSet();
}
SetMasterControl(oDataSet, eControlType);
}
/// <summary>
/// 控件初始化
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
base.RowHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(MasterControl_RowHeaderMouseClick);
base.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(MasterControl_RowPostPaint);
base.Scroll += new System.Windows.Forms.ScrollEventHandler(MasterControl_Scroll);
base.SelectionChanged += new System.EventHandler(MasterControl_SelectionChanged);
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MasterControl));
this.RowHeaderIconList = new System.Windows.Forms.ImageList(this.components);
((System.ComponentModel.ISupportInitialize)this).BeginInit();
this.SuspendLayout();
//
//RowHeaderIconList
//
this.RowHeaderIconList.ImageStream = (System.Windows.Forms.ImageListStreamer)(resources.GetObject("RowHeaderIconList.ImageStream"));
this.RowHeaderIconList.TransparentColor = System.Drawing.Color.Transparent;
this.RowHeaderIconList.Images.SetKeyName(, "expand.png");
this.RowHeaderIconList.Images.SetKeyName(, "collapse.png");
//
//MasterControl
//
((System.ComponentModel.ISupportInitialize)this).EndInit();
this.ResumeLayout(false);
}
#endregion
#region 数据绑定
/// <summary>
/// 设置表之间的主外键关联
/// </summary>
/// <param name="tableName">DataTable的表名称</param>
/// <param name="foreignKey">外键</param>
public void setParentSource(string tableName, string primarykey, string foreignKey)
{
this.DataSource = new DataView(_cDataset.Tables[tableName]);
cModule.setGridRowHeader(this);
_foreignKey = foreignKey;
_primaryKey = primarykey;
if (_cDataset.Tables[tableName].Columns[primarykey].GetType().ToString() == typeof(int).ToString()
|| _cDataset.Tables[tableName].Columns[primarykey].GetType().ToString() == typeof(double).ToString()
|| _cDataset.Tables[tableName].Columns[primarykey].GetType().ToString() == typeof(decimal).ToString())
{
_filterFormat = foreignKey + "={}";
}
else
{
_filterFormat = foreignKey + "=\'{}\'";
}
}
#endregion
#region 事件
//控件的行头点击事件
private void MasterControl_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
Rectangle rect = new Rectangle(System.Convert.ToInt((double)(rowDefaultHeight - ) / ), System.Convert.ToInt((double)(rowDefaultHeight - ) / ), , );
if (rect.Contains(e.Location))
{
//缩起
if (rowCurrent.Contains(e.RowIndex))
{
rowCurrent.Clear();
this.Rows[e.RowIndex].Height = rowDefaultHeight;
this.Rows[e.RowIndex].DividerHeight = rowDefaultDivider;
this.ClearSelection();
collapseRow = true;
this.Rows[e.RowIndex].Selected = true;
if (EControlType == controlType.middle)
{
var oParent = ((MasterControl)this.Parent.Parent);
oParent.Rows[oParent.ExpandRowIndex].Height = rowDefaultHeight * (this.Rows.Count + );
oParent.Rows[oParent.ExpandRowIndex].DividerHeight = rowDefaultHeight * (this.Rows.Count + );
if (oParent.Rows[oParent.ExpandRowIndex].Height > )
{
oParent.Rows[oParent.ExpandRowIndex].Height = ;
oParent.Rows[oParent.ExpandRowIndex].Height = ;
}
}
}
//展开
else
{
if (!(rowCurrent.Count == ))
{
var eRow = rowCurrent[];
rowCurrent.Clear();
this.Rows[eRow].Height = rowDefaultHeight;
this.Rows[eRow].DividerHeight = rowDefaultDivider;
this.ClearSelection();
collapseRow = true;
this.Rows[eRow].Selected = true;
}
rowCurrent.Add(e.RowIndex);
this.ClearSelection();
collapseRow = true;
this.Rows[e.RowIndex].Selected = true;
this.ExpandRowIndex = e.RowIndex;
this.Rows[e.RowIndex].Height = + rowDefaultHeight * (((DataView)(childView.childGrid[].DataSource)).Count + );
this.Rows[e.RowIndex].DividerHeight = + rowDefaultHeight * (((DataView)(childView.childGrid[].DataSource)).Count);
//设置一个最大高度
if (this.Rows[e.RowIndex].Height > )
{
this.Rows[e.RowIndex].Height = ;
this.Rows[e.RowIndex].DividerHeight = ;
}
if (EControlType == controlType.middle)
{
if (this.Parent.Parent.GetType() != typeof(MasterControl))
return;
var oParent = ((MasterControl)this.Parent.Parent);
oParent.Rows[oParent.ExpandRowIndex].Height = this.Rows[e.RowIndex].Height + rowDefaultHeight * (this.Rows.Count + );
oParent.Rows[oParent.ExpandRowIndex].DividerHeight = this.Rows[e.RowIndex].DividerHeight + rowDefaultHeight * (this.Rows.Count + );
if (oParent.Rows[oParent.ExpandRowIndex].Height > )
{
oParent.Rows[oParent.ExpandRowIndex].Height = ;
oParent.Rows[oParent.ExpandRowIndex].Height = ;
}
}
//if (EControlType == controlType.outside)
//{
// //SetControl(this);
//}
//this.Rows[e.RowIndex].Height = rowExpandedHeight;
//this.Rows[e.RowIndex].DividerHeight = rowExpandedDivider;
}
//this.ClearSelection();
//collapseRow = true;
//this.Rows[e.RowIndex].Selected = true;
}
else
{
collapseRow = false;
}
}
catch (Exception ex)
{
}
}
//控件的行重绘事件
private void MasterControl_RowPostPaint(object obj_sender, DataGridViewRowPostPaintEventArgs e)
{
try
{
var sender = (DataGridView)obj_sender;
//set childview control
var rect = new Rectangle((int)(e.RowBounds.X + ((double)(rowDefaultHeight - ) / )), (int)(e.RowBounds.Y + ((double)(rowDefaultHeight - ) / )), , );
if (collapseRow)
{
if (this.rowCurrent.Contains(e.RowIndex))
{
#region 更改点开后背景色 刘金龙
var rect = new Rectangle(e.RowBounds.X, e.RowBounds.Y + rowDefaultHeight, e.RowBounds.Width, e.RowBounds.Height - rowDefaultHeight);
using (Brush b = new SolidBrush(Color.FromArgb(, , )))
{
e.Graphics.FillRectangle(b, rect);
}
#endregion
sender.Rows[e.RowIndex].DividerHeight = sender.Rows[e.RowIndex].Height - rowDefaultHeight;
e.Graphics.DrawImage(RowHeaderIconList.Images[(int)rowHeaderIcons.collapse], rect);
childView.Location = new Point(e.RowBounds.Left + sender.RowHeadersWidth, e.RowBounds.Top + rowDefaultHeight + );
childView.Width = e.RowBounds.Right - sender.RowHeadersWidth;
childView.Height = System.Convert.ToInt(sender.Rows[e.RowIndex].DividerHeight - );
childView.Visible = true;
}
else
{
childView.Visible = false;
e.Graphics.DrawImage(RowHeaderIconList.Images[(int)rowHeaderIcons.expand], rect);
}
collapseRow = false;
}
else
{
if (this.rowCurrent.Contains(e.RowIndex))
{
#region 更改点开后背景色 刘金龙
var rect = new Rectangle(e.RowBounds.X, e.RowBounds.Y + rowDefaultHeight, e.RowBounds.Width, e.RowBounds.Height - rowDefaultHeight);
using (Brush b = new SolidBrush(Color.FromArgb(,,)))
{
e.Graphics.FillRectangle(b, rect);
}
#endregion
sender.Rows[e.RowIndex].DividerHeight = sender.Rows[e.RowIndex].Height - rowDefaultHeight;
e.Graphics.DrawImage(RowHeaderIconList.Images[(int)rowHeaderIcons.collapse], rect);
childView.Location = new Point(e.RowBounds.Left + sender.RowHeadersWidth, e.RowBounds.Top + rowDefaultHeight + );
childView.Width = e.RowBounds.Right - sender.RowHeadersWidth;
childView.Height = System.Convert.ToInt(sender.Rows[e.RowIndex].DividerHeight - );
childView.Visible = true;
}
else
{
childView.Visible = false;
e.Graphics.DrawImage(RowHeaderIconList.Images[(int)rowHeaderIcons.expand], rect);
}
}
cModule.rowPostPaint_HeaderCount(sender, e);
}
catch
{
}
}
//控件的滚动条滚动事件
private void MasterControl_Scroll(object sender, ScrollEventArgs e)
{
try
{
if (!(rowCurrent.Count == ))
{
collapseRow = true;
this.ClearSelection();
this.Rows[rowCurrent[]].Selected = true;
}
}
catch
{
}
}
//控件的单元格选择事件
private void MasterControl_SelectionChanged(object sender, EventArgs e)
{
try
{
if (!(this.RowCount == ))
{
if (rowCurrent.Contains(this.CurrentRow.Index))
{
foreach (DataGridView cGrid in childView.childGrid)
{
((DataView)cGrid.DataSource).RowFilter = string.Format(_filterFormat, this[_primaryKey, this.CurrentRow.Index].Value);
}
}
}
}
catch
{
}
}
#endregion
#region Private
//设置构造函数的参数
private void SetMasterControl(DataSet cDataset, controlType eControlType)
{
//.控件初始化赋值
this.Controls.Add(childView);
InitializeComponent();
_cDataset = cDataset;
childView._cDataset = cDataset;
cModule.applyGridTheme(this);
Dock = DockStyle.Fill;
EControlType = eControlType;
this.AllowUserToAddRows = false;
//.通过读取DataSet里面的Relations得到表的关联关系
if (cDataset.Relations.Count <= )
{
return;
}
DataRelation oRelates;
if (eControlType == controlType.outside)
{
oRelates = cDataset.Relations[];
childView.Add(oRelates.ParentTable.TableName, oRelates.ParentColumns[].ColumnName, oRelates.ChildColumns[].ColumnName);
}
else if (eControlType == controlType.middle)
{
oRelates = cDataset.Relations[cDataset.Relations.Count - ];
childView.Add(oRelates.ChildTable.TableName);
}
//.设置主外键对应关系
oRelates = cDataset.Relations[];
//主表里面的值,副表里面的过滤字段
setParentSource(oRelates.ParentTable.TableName,oRelates.ParentColumns[].ColumnName, oRelates.ChildColumns[].ColumnName);
}
private void SetControl(MasterControl oGrid)
{
oGrid.childView.RemoveControl();
//oGrid.childView.Controls.RemoveByKey("ChildrenMaster");
//
//var oRelates = _cDataset.Relations[];
//oGrid.childView.Add(oRelates.ParentTable.TableName, oRelates.ChildColumns[].ColumnName);
//foreach (var oGridControl in oGrid.Controls)
//{
// if (oGridControl.GetType() != typeof(detailControl))
// {
// continue;
// }
// var DetailControl =(detailControl)oGridControl;
// foreach (var odetailControl in DetailControl.Controls)
// {
// if (odetailControl.GetType() != typeof(MasterControl))
// {
// continue;
// }
// var OMasterControl = (MasterControl)odetailControl;
// foreach (var oMasterControl in OMasterControl.Controls)
// {
// if (oMasterControl.GetType() == typeof(detailControl))
// {
// ((detailControl)oMasterControl).Visible = false;
// return;
// }
// }
// }
//}
}
//将List集合转换成DataTable
private DataTable Fill(object obj)
{
if(!(obj is IList))
{
return null;
}
var objlist = obj as IList;
if (objlist == null || objlist.Count <= )
{
return null;
}
var tType = objlist[];
DataTable dt = new DataTable(tType.GetType().Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = tType.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var t in objlist)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = , j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
#endregion
} 

2、detailControl变量作为一个容器用来保存子表格

代码如下:

public class detailControl : Ewin.Client.Frame.Controls.EwinPanel
{
#region 字段
public List<DataGridView> childGrid = new List<DataGridView>();
public DataSet _cDataset;
#endregion
#region 方法
public void Add(string tableName, string strPrimaryKey, string strForeignKey)
{
//TabPage tPage = new TabPage() { Text = pageCaption };
//this.Controls.Add(tPage);
var newGrid = new MasterControl(_cDataset, controlType.middle) { Dock = DockStyle.Fill, DataSource = new DataView(_cDataset.Tables[tableName]) };
newGrid.setParentSource(tableName, strPrimaryKey, strForeignKey);//设置主外键
//newGrid.Name = "ChildrenMaster";
//tPage.Controls.Add(newGrid);
this.Controls.Add(newGrid);
//this.BorderStyle = BorderStyle.FixedSingle;
cModule.applyGridTheme(newGrid);
cModule.setGridRowHeader(newGrid);
newGrid.RowPostPaint += cModule.rowPostPaint_HeaderCount;
childGrid.Add(newGrid);
}
public void Add(string tableName)
{
//TabPage tPage = new TabPage() { Text = pageCaption };
//this.Controls.Add(tPage);
DataGridView newGrid = new Ewin.Client.Frame.Controls.EwinGrid() { Dock = DockStyle.Fill, DataSource = new DataView(_cDataset.Tables[tableName]) };
newGrid.AllowUserToAddRows = false;
//tPage.Controls.Add(newGrid);
this.Controls.Add(newGrid);
cModule.applyGridTheme(newGrid);
cModule.setGridRowHeader(newGrid);
newGrid.RowPostPaint += cModule.rowPostPaint_HeaderCount;
childGrid.Add(newGrid);
}
public void RemoveControl()
{
this.Controls.Remove(childGrid[]);
childGrid.Clear();
}
#endregion
} 

3、cModule.cs用来设置样式

namespace Ewin.Client.Frame.UcGrid
{
/// <summary>
/// 折叠控件样式以及行数操作类
/// </summary>
sealed class cModule
{
#region CustomGrid
static System.Windows.Forms.DataGridViewCellStyle dateCellStyle = new System.Windows.Forms.DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight };
static System.Windows.Forms.DataGridViewCellStyle amountCellStyle = new System.Windows.Forms.DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight, Format = "N" };
static System.Windows.Forms.DataGridViewCellStyle gridCellStyle = new System.Windows.Forms.DataGridViewCellStyle
{
Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft,
BackColor = System.Drawing.Color.FromArgb(System.Convert.ToInt(System.Convert.ToByte()), System.Convert.ToInt(System.Convert.ToByte()), System.Convert.ToInt(System.Convert.ToByte())),
Font = new System.Drawing.Font("Segoe UI", (float)(.F), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte()),
ForeColor = System.Drawing.SystemColors.ControlLightLight,
SelectionBackColor = System.Drawing.SystemColors.Highlight,
SelectionForeColor = System.Drawing.SystemColors.HighlightText,
WrapMode = System.Windows.Forms.DataGridViewTriState.True
};
static System.Windows.Forms.DataGridViewCellStyle gridCellStyle = new System.Windows.Forms.DataGridViewCellStyle
{
Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft,
BackColor = System.Drawing.SystemColors.ControlLightLight,
Font = new System.Drawing.Font("Segoe UI", (float)(.F), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte()),
ForeColor = System.Drawing.SystemColors.ControlText,
SelectionBackColor = System.Drawing.Color.FromArgb(System.Convert.ToInt(System.Convert.ToByte()), System.Convert.ToInt(System.Convert.ToByte()), System.Convert.ToInt(System.Convert.ToByte())),
SelectionForeColor = System.Drawing.SystemColors.HighlightText,
WrapMode = System.Windows.Forms.DataGridViewTriState.False
};
static System.Windows.Forms.DataGridViewCellStyle gridCellStyle = new System.Windows.Forms.DataGridViewCellStyle
{
Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft,
BackColor = System.Drawing.Color.WhiteSmoke,
Font = new System.Drawing.Font("Segoe UI", (float)(.F), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte()),
ForeColor = System.Drawing.SystemColors.WindowText,
SelectionBackColor = System.Drawing.Color.FromArgb(System.Convert.ToInt(System.Convert.ToByte()), System.Convert.ToInt(System.Convert.ToByte()), System.Convert.ToInt(System.Convert.ToByte())),
SelectionForeColor = System.Drawing.SystemColors.HighlightText,
WrapMode = System.Windows.Forms.DataGridViewTriState.True
};
//设置表格的主题样式
static public void applyGridTheme(DataGridView grid)
{
grid.AllowUserToAddRows = false;
grid.AllowUserToDeleteRows = false;
grid.BackgroundColor = System.Drawing.SystemColors.Window;
grid.BorderStyle = System.Windows.Forms.BorderStyle.None;
grid.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
grid.ColumnHeadersDefaultCellStyle = gridCellStyle;
grid.ColumnHeadersHeight = ;
grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
grid.DefaultCellStyle = gridCellStyle;
grid.EnableHeadersVisualStyles = false;
grid.GridColor = System.Drawing.SystemColors.GradientInactiveCaption;
//grid.ReadOnly = true;
grid.RowHeadersVisible = true;
grid.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
grid.RowHeadersDefaultCellStyle = gridCellStyle;
grid.Font = gridCellStyle.Font;
}
//设置表格单元格样式
static public void setGridRowHeader(DataGridView dgv, bool hSize = false)
{
dgv.TopLeftHeaderCell.Value = "NO ";
dgv.TopLeftHeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgv.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
foreach (DataGridViewColumn cCol in dgv.Columns)
{
if (cCol.ValueType.ToString() == typeof(DateTime).ToString())
{
cCol.DefaultCellStyle = dateCellStyle;
}
else if (cCol.ValueType.ToString() == typeof(decimal).ToString() || cCol.ValueType.ToString() == typeof(double).ToString())
{
cCol.DefaultCellStyle = amountCellStyle;
}
}
if (hSize)
{
dgv.RowHeadersWidth = dgv.RowHeadersWidth + ;
}
dgv.AutoResizeColumns();
}
//设置表格的行号
static public void rowPostPaint_HeaderCount(object obj_sender, DataGridViewRowPostPaintEventArgs e)
{
try
{
var sender = (DataGridView)obj_sender;
//set rowheader count
DataGridView grid = (DataGridView)sender;
string rowIdx = System.Convert.ToString((e.RowIndex + ).ToString());
var centerFormat = new StringFormat();
centerFormat.Alignment = StringAlignment.Center;
centerFormat.LineAlignment = StringAlignment.Center;
Rectangle headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top,
grid.RowHeadersWidth, e.RowBounds.Height - sender.Rows[e.RowIndex].DividerHeight);
e.Graphics.DrawString(rowIdx, grid.Font, SystemBrushes.ControlText,
headerBounds, centerFormat);
}
catch (Exception ex)
{
}
}
#endregion
}
/// <summary>
/// 控件类型,是最外层的表格还是中间层的表格
/// </summary>
public enum controlType
{
outside = ,
middle =
}
/// <summary>
/// 展开图标
/// </summary>
public enum rowHeaderIcons
{
expand = ,
collapse =
}
}

4、From页面调用

#region 使用方法一

//var oDataSet = GetDataSet();
//
//masterDetail = new MasterControl(oDataSet, controlType.outside);
#endregion

#region 使用方法二

var dicRelateData1 = new Dictionary<string, string>();
var dicRelateData2 = new Dictionary<string, string>();
dicRelateData1.Add("Menu_ID","Menu_ID");//表格一和表格二之间的主外键关系
dicRelateData2.Add("Menu_Name2","Menu_Name2");//表格二和表格三之间的主外键关系
masterDetail = new MasterControl(GetDataSource(), GetDataSource2(), GetDataSource3(), dicRelateData1, dicRelateData2, controlType.outside); #endregion panelView.Controls.Add(masterDetail); 

昨天应领导要求,折叠控件增加了折叠线的效果,看起来有没有更加像模像样了。~~~

其实就在行重绘事件private void MasterControl_RowPostPaint(object obj_sender, DataGridViewRowPostPaintEventArgs e)里面增加了如下代码:

using (Pen p = new Pen(Color.GhostWhite))
{
var iHalfWidth = (e.RowBounds.Left + sender.RowHeadersWidth) / 2;
var oPointHLineStart = new Point(rect1.X + iHalfWidth, rect1.Y);
var oPointHLineEnd = new Point(rect1.X + iHalfWidth, rect1.Y + rect1.Height / 2);
e.Graphics.DrawLine(p, oPointHLineStart, oPointHLineEnd);
//折叠线
e.Graphics.DrawLine(p, oPointHLineEnd, new Point(oPointHLineEnd.X + iHalfWidth, oPointHLineEnd.Y));
}

效果如下:

以上所述是小编给大家介绍的WinForm中DataGridView折叠控件的相关知识,希望对大家有所帮助!

(0)

相关推荐

  • WinForm DataGridView控件隔行变色的小例子

    复制代码 代码如下: dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.White; 隔行变色 复制代码 代码如下: /// <summary>         /// 隔行变色         /// </summary>         /// <param name="dgv">传入DataGridView控件名称</param>         public

  • WinForm中DataGridView添加,删除,修改操作具体方法

    1.添加操作,代码如下: 复制代码 代码如下: IList<SelfRun> selfRunConfigs = new List<SelfRun>();private void btnNewConfig_Click(object sender, EventArgs e){try{string _lampNo = UpDownSelfLampNo.Value.ToString();int _ctrlGpNo = Convert.ToInt16(UpDownCtrlGpCnt.Valu

  • winform用datagridview制作课程表实例

    本文实例讲述了winform用datagridview制作课程表的方法.分享给大家供大家参考.具体分析如下: 课程表的最终效果如下图所示: 具体方法如下: 1.首先定义一个datatable,并添加列集,行集. 一张课程表的表结构就出来了.(如上图表结构式4行8列)只有表有结构.有数据才可以绑到 datagridivew控件里.否则绑上了,也没得显示. 代码如下: 复制代码 代码如下: int num , week ;   // 周数节数,第二步的时候用得到. DataTable dt = ne

  • WinForm中DataGridView折叠控件【超好看】

    刚到一家新公司,领导下发任务要用cs系统做一个表格折叠显示,这真是把我难倒了,自己工作6年一直以来都是做BS的系统.这如果在BS里面那太简单了,JqGrid默认都自带,可是DataGridview不支持折叠啊.自己一点经验没有,怎么办呢?于是上网搜了相关视频,资料,开始学习起来.最后借鉴源码封了这么一个东西,发出来分享下,也能让自己加深印象. 首先不多说,上图.如果大家感谢还不错,请继续往下阅读: 大概的效果就是这样. 上代码. 1.首先重写DataGridview,代码如下: public c

  • 简介Winform中创建用户控件

    步骤: 1.创建一个项目,该项目主要用来设计用户控件. 2.创建一个用户控件窗体,用来设计用户控件. 3.向用户控件窗体中添加一个按钮(button1),给它添加相应的移入.移出事件,实现移入时按钮的背景设置一个图片,移出的时候背景设置另一个图片. 复制代码 代码如下: private void button1_MouseEnter(object sender, EventArgs e)        {            this.button1.Image = Image.FromFil

  • WPF中引入WindowsForms控件的方法

    本文实例讲述了WPF中引入WindowsForms控件的方法.分享给大家供大家参考,具体如下: 环境: [1]WindowsXP with SP3 [2]VS2008 with SP1 正文: Step1:在现有工程中引入Windows Forms 鼠标右键[References]->选择[Add Reference]->[.NET]标签页 加入[WindowsFormsIntegration]和[System.Windows.Forms]两项 Step2:在XAML文件里加入 [S2-1]加

  • C#之WinForm跨线程访问控件实例

    本文实例讲述了C#中WinForm跨线程访问控件的实现方法,分享给大家供大家参考. 具体实现方法如下: 1.跨线程访问控件委托和类的定义 复制代码 代码如下: using System; using System.Windows.Forms; namespace ahwildlife.Utils {     /// <summary>     /// 跨线程访问控件的委托     /// </summary>     public delegate void InvokeDeleg

  • Flutter折叠控件使用方法详解

    本文实例为大家分享了Flutter折叠控件使用的具体代码,供大家参考,具体内容如下 1.官方折叠控件ExpansionTiles 官方默认提供了一个折叠控件 ExpansionTiles 主要用于listView做折叠和展开操作的,先来看看一般的用法 Widget _buildTiles(Entry root) {     return new ExpansionTile(       title: new Text(root.title),       children: root.child

  • WinForm入门与基本控件使用详解

    目录 一.Winform入门 1.WinForm项目结构 2.窗口设计与控件布局 3.窗口事件 4.时间显示器小练习 二.WinForm布局开发 1. 手动布局解决自适应问题 2.WinForm布局属性 3.WinForm布局器 三.WinForm常用控件 1.界面展示 2.实体类 Student(封装信息) 3.逻辑事件代码Form.cs 四.图片框与项目资源 1.添加资源 2.图片框控件展示 一.Winform入门 WinForm 是 Windows Form 的简称,是基于 .NET Fr

  • WinForm实现移除控件某个事件的方法

    本文实例讲述了WinForm实现移除控件某个事件的方法,供大家参考借鉴一下.具体功能代码如下: 主要功能部分代码如下: /// <summary> /// 移除控件某个事件 /// </summary> /// <param name="control">控件</param> /// <param name="eventName">需要移除的控件名称eg:EventClick</param> p

  • Asp.net 中使用GridView控件实现Checkbox单选

    在GridView控件中,第0列有放一个CheckBox控件,现想实现对CheckBox进行单选. 先看看效果: 在ASPX页面,可以这样做: 有一点注意的是需要使用OnRowCreated事件. 在ASPX.cs代码里,实现上面OnRowCreated事件: 上面有个事件委托: Ok,特简单的.全部使用服务端来实现,或许前端js也能实现. 以上所述是小编给大家介绍的Asp.net 中使用GridView控件实现Checkbox单选,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回

  • asp.net中使用 Repeater控件拖拽实现排序并同步数据库字段排序

    数据库表中有一个单位表,里面包括ID.Name.Order等字段,现在有个后台管理功能,可以设置这些单位在某些统计表格中的先后显示顺序,于是想到用拖拽方式实现,这样操作起来更简便. 使用了GifCam软件做了一个示例动画,效果如下图所示: 于是就动手起来,发现jquery.ui中提供sortable函数,可用于排序,界面中从数据库绑定的单位使用Repeater控件,下面简单介绍下主要步骤: 1.项目中使用到的jquery-1.7.2.min.js和jquery-ui.min.js请点击进行下载,

  • Android中findViewById获取控件返回为空问题怎么解决

    在Android程序中,有时候需要加载非原来activity中xml布局中的控件,来使Android程序的界面更加丰富. 我本身是在使用ViewFlipper中遇到的问题. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); inflater=(LayoutInflater)getSystemService(LAYO

随机推荐