WinForm中DataGridView折叠控件【超好看】
网络编程 2021-07-04 22:41www.168986.cn编程入门
刚到一家新公司,领导下发任务要用cs系统做一个表格折叠显示,这真是把我难倒了,自己工作6年一直以来都是做BS的系统,于是上网搜了相关视频,资料,开始学习起来,狼蚁网站SEO优化长沙网络推广把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 ponents = 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.ponents = 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.ponents); ((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); /
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程