/// <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> /// 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); } }