/// <summary> /// Edit Button OnClick Event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EditClick(object sender, EventArgs e) { ToolStripButton button = sender as ToolStripButton; ComponentToolbar tool = null; ComponentDataGrid grid = null; if (button != null) { tool = button.GetCurrentParent() as ComponentToolbar; grid = tool.DataGrid; } else { grid = sender as ComponentDataGrid; } var rows = grid.SelectedRows; if (rows == null || rows.Count == 0) { MessageBox.Show("当前没有可以编辑的数据!", "title", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } frmModule form = new frmModule(grid, true); form.atRefresh = RefreshClick; form.InitializeData(); form.ShowDialog(); }
private void BindMenuStrip(DataGridView grid) { var menu = new ContextMenuStrip(); menu.Items.Add("复制"); menu.Items.Add("查看当前数据源"); menu.ItemClicked += (s, e) => { ContextMenuStrip toolstrip = e.ClickedItem.GetCurrentParent() as ContextMenuStrip; ComponentDataGrid getDgv = toolstrip.SourceControl as ComponentDataGrid; toolstrip.Hide(); if (e.ClickedItem.Text.Equals("查看当前数据源")) { new frmDataSource(grid.Name, (getDgv.DataSource as DataTable).Namespace).ShowDialog(); } else if (e.ClickedItem.Text.Equals("复制")) { Clipboard.SetText(getDgv.CurrentCell.Value.ToString(), TextDataFormat.UnicodeText); } }; grid.ContextMenuStrip = menu; }
/// <summary> /// bind cell on click event on data container /// </summary> /// <param name="dataGrid">container</param> public void BindingCellClickEvent(ComponentDataGrid Grid) { Grid.CellClick += CommonCellClick; Grid.CellDoubleClick += CommonCellDoubleClick; Grid.RowPostPaint += CommonRowPostPaint; BindMenuStrip(Grid); CommonCellClick(Grid, null); }
/// <summary> /// 创建一个数据容器 /// </summary> /// <param name="Data"></param> /// <param name="DataColumnInfos"></param> /// <returns></returns> internal static ComponentDataGrid NewDataGrid(DataTable Data, DataTable DataColumnInfos) { var grid = new ComponentDataGrid(); grid.AutoGenerateColumns = false; grid.DataSource = Data; InitStyle(grid, DataColumnInfos); return(grid); }
/// <summary> /// Clear child container's rows /// </summary> /// <param name="dataGrid">Child container</param> private void ClearRows(ComponentDataGrid dataGrid) { List <ComponentDataGrid> childrenGrid = dataGrid.GetChildrenGrid(); foreach (ComponentDataGrid grid in childrenGrid) { DataTable dt = grid.DataSource as DataTable; if (dt == null) { continue; } dt.Rows.Clear(); grid.DataSource = dt; ClearRows(grid); } }
/// <summary> /// 初始化CompontentDataGrid样式 /// </summary> /// <param name="MainDgv"></param> public static void InitStyle(ComponentDataGrid dgv, DataTable ColData) { dgv.ReadOnly = true; dgv.BackgroundColor = Color.White; dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dgv.AllowUserToAddRows = false; dgv.CellBorderStyle = DataGridViewCellBorderStyle.None; dgv.AdvancedColumnHeadersBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None; dgv.AdvancedColumnHeadersBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None; dgv.AdvancedColumnHeadersBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None; dgv.AdvancedColumnHeadersBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None; dgv.TopLeftHeaderCell.Value = "行号"; DataTable dt = dgv.DataSource as DataTable; foreach (DataColumn dc in dt.Columns) { Type type = dc.DataType; DataGridViewColumn col = null; if (type.Equals(typeof(bool))) { col = new DataGridViewCheckBoxColumn { CellTemplate = new DataGridViewCheckBoxCell() }; } else { col = new DataGridViewTextBoxColumn { CellTemplate = new DataGridViewTextBoxCell() }; } var colinfo = new ColumnInfo() { ReadOnly = GetValue <bool>(ColData, "freadonly", dc.ColumnName), Visible = GetValue <bool>(ColData, "fvisiable", dc.ColumnName), ColumnName = dc.ColumnName, DefaultValue = GetValue <string>(ColData, "fdefaultvalue", dc.ColumnName), Translation = GlobalInvariant.GetLanguageByKey(dc.ColumnName), DataPropertyName = dc.ColumnName, Num = GetValue <int>(ColData, "fnum", dc.ColumnName), }; col.Name = colinfo.ColumnName; col.DisplayIndex = colinfo.Num; col.Visible = colinfo.Visible; col.ReadOnly = colinfo.ReadOnly; col.DataPropertyName = colinfo.ColumnName; col.HeaderText = colinfo.Translation; col.Tag = colinfo; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dgv.Columns.Add(col); } T GetValue <T>(DataTable Data, string MatchColumn, string ColumnName) { var row = Data.Select($"fcolname='{ColumnName}'").FirstOrDefault(); if (row != null) { return((T)row[MatchColumn]); } return(default(T)); } }
/// <summary> /// Data container's cell on click event /// </summary> /// <param name="sender">container</param> /// <param name="e">data args</param> private void CommonCellClick(object sender, DataGridViewCellEventArgs e) { if (e != null && e.RowIndex < 0) { return; } ComponentDataGrid dataGrid = sender as ComponentDataGrid; if (e == null) { ToolbarAddEvent(dataGrid.ToolBar); } if (dataGrid == null) { throw new ArgumentNullException("当前容器为空"); } List <ComponentDataGrid> childrenGrid = dataGrid.GetChildrenGrid(); var rows = dataGrid.SelectedRows; if (rows == null || rows.Count == 0) { foreach (ComponentDataGrid grid in childrenGrid) { DataTable dt = grid.DataSource as DataTable; if (dt == null) { continue; } dt.Rows.Clear(); grid.DataSource = dt; ClearRows(grid); } return; } var row = rows[0]; foreach (ComponentDataGrid grid in childrenGrid) { string[] childKeys = grid.PrimaryKey; string[] parentKeys = grid.PrePrimaryKey; string swherestring = GetWhereString(row, parentKeys, childKeys); grid.PrePrimaryKeyValues = new string[parentKeys.Length]; for (int i = 0; i < parentKeys.Length; i++) { grid.PrePrimaryKeyValues[i] = row.Cells[parentKeys[i]].Value + ""; } int iindex = (grid.DataSource as DataTable).Namespace.LastIndexOf("where", StringComparison.OrdinalIgnoreCase); string sql = string.Empty; if (iindex < 0) { sql = (grid.DataSource as DataTable).Namespace + "\r\n where " + swherestring; } else { sql = (grid.DataSource as DataTable).Namespace.Substring(0, iindex).TrimEnd() + "\r\n where " + swherestring; } string tablename = (grid.DataSource as DataTable).TableName; DataTable dt = DBHelper.GetDataTable(sql); dt.Namespace = sql; dt.TableName = tablename; grid.DataSource = dt; if (!_mlistEventRegist.Contains(grid.Name)) { grid.CellClick += CommonCellClick; grid.CellDoubleClick += CommonCellDoubleClick; grid.RowPostPaint += CommonRowPostPaint; BindMenuStrip(grid); _mlistEventRegist.Add(grid.Name); } CommonCellClick(grid, e); } }
/// <summary> /// 获取数据容器的DataTable /// </summary> /// <param name="Grid">数据容器</param> /// <returns></returns> public static DataTable GetDataTable(this ComponentDataGrid Grid) { return(Grid.DataSource as DataTable); }
private void RecursiveForm(ComponentDataGrid parentGrid, DataTable dtRelation, string mainName) { DataRow[] drs = dtRelation.Select($"fFatherName ='{mainName}'"); //第二级是否有多个gird bool bSecondOnly = drs?.Count() > 1 ? true : false; ComponentPanel tmpMain = new ComponentPanel(); var rows = parentGrid.SelectedRows; var row = rows[0]; //二级存在多个 if (bSecondOnly) { MainTab.SelectedTab.Controls.Add(tmpMain); tmpMain.Dock = DockStyle.Fill; tmpMain.BringToFront(); foreach (DataRow dr in drs) { string smainSql = $@"select * from t_program where fname ='{dr["fchildname"] + "" }'"; DataTable dtProgramInfo = DBHelper.GetDataTable(smainSql); DataRow drProgramInfo = dtProgramInfo.Rows[0]; string smainSql1 = drProgramInfo["fSql"].ToString().TrimEnd(); DataTable dtMain = DBHelper.GetDataTable(smainSql1); dtMain.TableName = drProgramInfo["fTable"] + ""; dtMain.Namespace = $"select * from (\r\n{smainSql1}\r\n) tfinal \r\nwhere 1=1"; ComponentDataGrid childGrid = tmpMain.InitializeNewTabPage(drProgramInfo["fcnname"] + "", drProgramInfo["fname"] + "", dtMain); var Keys = GetPrimary(dr); childGrid.PreGrid = parentGrid; childGrid.PrimaryKey = Keys.ChildKeys; childGrid.PrePrimaryKey = Keys.ParentKeys; parentGrid.AddNextGrid(childGrid); } } else { //从属表数据 DataRow drRelation = drs[0]; var Keys = GetPrimary(drRelation); string smainSql = $@"select * from t_program where fname ='{drRelation["fchildname"] + "" }'"; DataTable dtProgramInfo = DBHelper.GetDataTable(smainSql); DataRow drProgramInfo = dtProgramInfo.Rows[0]; string smainSql1 = drProgramInfo["fSql"] + ""; DataTable dtMain = DBHelper.GetDataTable(smainSql1); dtMain.TableName = drProgramInfo["fTable"] + ""; dtMain.Namespace = $"select * from (\r\n{smainSql1}\r\n) tfinal \r\nwhere 1=1"; ComponentDataGrid childGrid = tmpMain.InitializeNewTabPage(drProgramInfo["fcnname"] + "", drProgramInfo["fname"] + "", dtMain); MainTab.SelectedTab.Controls.Add(tmpMain); tmpMain.BringToFront(); childGrid.PreGrid = parentGrid; childGrid.PrimaryKey = Keys.ChildKeys; childGrid.PrePrimaryKey = Keys.ParentKeys; parentGrid.AddNextGrid(childGrid); DataRow[] drNeedRecursive = dtRelation.Select($"ffathername='{drProgramInfo["fname"] + ""}'"); if (drNeedRecursive.Count() > 0) { tmpMain.Dock = DockStyle.Top; SplitterControl splitter = new SplitterControl(); MainTab.SelectedTab.Controls.Add(splitter); splitter.Dock = DockStyle.Top; splitter.BringToFront(); RecursiveForm(childGrid, dtRelation, drProgramInfo["fname"] + ""); } else { tmpMain.Dock = DockStyle.Fill; } } }
/// <summary> /// 初始化窗体 /// </summary> /// <param name="SourceFormName">窗体Name</param> private void InitForm(string SourceFormName) { string smainSql = $@"select * from t_program where fname ='{SourceFormName}'"; DataTable dtProgramInfo = DBHelper.GetDataTable(smainSql); DataRow drProgramInfo = dtProgramInfo.Rows[0]; string smainSql1 = $"select * from (\r\n {drProgramInfo["fSql"] + ""}\r\n)t1 where 1=1"; DataTable dtMain = DBHelper.GetDataTable(smainSql1); dtMain.TableName = drProgramInfo["fTable"] + ""; dtMain.Namespace = smainSql1; ComponentPanel panel = new ComponentPanel(true); ComponentDataGrid grid = panel.InitializeNewTabPage(drProgramInfo["fCNName"] + "", SourceFormName, dtMain); panel.Dock = DockStyle.Top; MainTab.SelectedTab.Controls.Add(panel); panel.BringToFront(); string srelationSql = $@"select * from T_FormRelationships with(nolock) where fmainname = '{SourceFormName}'"; DataTable dtRelationShip = DBHelper.GetDataTable(srelationSql); //无从属关系 if (dtRelationShip == null || dtRelationShip.Rows.Count == 0) { panel.Dock = DockStyle.Fill; EventHelper eventHelperOnly = new EventHelper(); eventHelperOnly.BindingCellClickEvent(grid); return; } SplitterControl splitter = new SplitterControl(); MainTab.SelectedTab.Controls.Add(splitter); splitter.Dock = DockStyle.Top; splitter.BringToFront(); RecursiveForm(grid, dtRelationShip, SourceFormName); #region 设置高度 int ictrlCount = MainTab.SelectedTab.Controls.OfType <ComponentPanel>().Count(); int ipageHeigh = MainTab.SelectedTab.Height; int iResult = ipageHeigh / ictrlCount; foreach (Control ctrl in MainTab.SelectedTab.Controls) { if (!(ctrl is ComponentPanel)) { continue; } ComponentPanel frm = ctrl as ComponentPanel; frm.Height = iResult; } #endregion #region 点击事件绑定 EventHelper eventHelper = new EventHelper(); eventHelper.BindingCellClickEvent(grid); #endregion }