//设置构造函数的参数 private void SetMasterControl(DataSet cDataset, ControlType eControlType) { //1.控件初始化赋值 this.Controls.Add(ChildView); InitializeComponent(); _cDataset = cDataset; ChildView.CDataset = cDataset; CModule.ApplyGridTheme(this); Dock = DockStyle.Fill; _eControlType = eControlType; this.AllowUserToAddRows = false; //2.通过读取DataSet里面的Relations得到表的关联关系 if (cDataset.Relations.Count <= 0) { return; } DataRelation oRelates; if (eControlType == ControlType.OutSide) { oRelates = cDataset.Relations[1]; ChildView.Add(oRelates.ParentTable.TableName, oRelates.ParentColumns[0].ColumnName, oRelates.ChildColumns[0].ColumnName); } else if (eControlType == ControlType.Middle) { oRelates = cDataset.Relations[cDataset.Relations.Count - 1]; ChildView.Add2(oRelates.ChildTable.TableName); } //3.设置主外键对应关系 oRelates = cDataset.Relations[0]; //主表里面的值,副表里面的过滤字段 SetParentSource(oRelates.ParentTable.TableName, oRelates.ParentColumns[0].ColumnName, oRelates.ChildColumns[0].ColumnName); }
public void Add2(string tableName) { //TabPage tPage = new TabPage() { Text = pageCaption }; //this.Controls.Add(tPage); DataGridView newGrid = new DataGridView { Dock = DockStyle.Fill, DataSource = new DataView(CDataset.Tables[tableName]), AllowUserToAddRows = false }; //tPage.Controls.Add(newGrid); this.Controls.Add(newGrid); CModule.ApplyGridTheme(newGrid); CModule.SetGridRowHeader(newGrid); newGrid.RowPostPaint += CModule.rowPostPaint_HeaderCount; ChildGrid.Add(newGrid); }
/// <summary> /// 设置表之间的主外键关联 /// </summary> /// <param name="tableName">DataTable的表名称</param> /// <param name="primarykey"></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(long).ToString() || _cDataset.Tables[tableName].Columns[primarykey].GetType().ToString() == typeof(decimal).ToString()) { _filterFormat = foreignKey + "={0}"; } else { _filterFormat = foreignKey + "=\'{0}\'"; } }
public void Add(string tableName, string strPrimaryKey, string strForeignKey) { //TabPage tPage = new TabPage() { Text = pageCaption }; //this.Controls.Add(tPage); MasterControl 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); }
//控件的行重绘事件 private void MasterControl_RowPostPaint(object objSender, DataGridViewRowPostPaintEventArgs e) { try { var sender = (DataGridView)objSender; //set childview control var rect = new Rectangle((int)(e.RowBounds.X + ((double)(RowDefaultHeight - 16) / 2)), (int)(e.RowBounds.Y + ((double)(RowDefaultHeight - 16) / 2)), 16, 16); if (CollapseRow) { if (this._rowCurrent.Contains(e.RowIndex)) { #region 更改点开后背景色 var rect1 = new Rectangle(e.RowBounds.X, e.RowBounds.Y + RowDefaultHeight, e.RowBounds.Width, e.RowBounds.Height - RowDefaultHeight); using (Brush b = new SolidBrush(Color.FromArgb(164, 169, 143))) { e.Graphics.FillRectangle(b, rect1); } 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)); } #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 + 5); ChildView.Width = e.RowBounds.Right - sender.RowHeadersWidth; ChildView.Height = Convert.ToInt32(sender.Rows[e.RowIndex].DividerHeight - 10); 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 rect1 = new Rectangle(e.RowBounds.X, e.RowBounds.Y + RowDefaultHeight, e.RowBounds.Width, e.RowBounds.Height - RowDefaultHeight); using (Brush b = new SolidBrush(Color.FromArgb(164, 169, 143))) { e.Graphics.FillRectangle(b, rect1); } 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)); } #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 + 5); ChildView.Width = e.RowBounds.Right - sender.RowHeadersWidth; ChildView.Height = Convert.ToInt32(sender.Rows[e.RowIndex].DividerHeight - 10); ChildView.Visible = true; } else { ChildView.Visible = false; e.Graphics.DrawImage(RowHeaderIconList.Images[(int)RowHeaderIcons.Expand], rect); } } CModule.rowPostPaint_HeaderCount(sender, e); } catch { // ignored } }