public static void InitDataGridViewColumns(DataGridView grid, DataRule dr) { if (grid == null || dr == null) { return; } grid.VirtualMode = true; DataTable table = dr.GetFieldRule(); if (table != null) { DataGridViewColumn gcolumn; DataRowCollection rows = table.Rows; if (grid.Columns.Count > 0) { grid.Columns.Clear(); } foreach (DataRow row in rows) { gcolumn = CreateDataGridViewColumn(RuleColumn.FromDataRow(row)); if (gcolumn != null) { grid.Columns.Add(gcolumn); } } grid.EditingControlShowing -= new DataGridViewEditingControlShowingEventHandler(DataGridViewEditingControlShowing); grid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(DataGridViewEditingControlShowing); } }
public static bool InitTableSchemaFromDataRule(DataTable table, DataRule dr, string alias) { if (table == null || dr == null) { return(false); } try { DataTable ruletable = dr.GetFieldRule(); if (ruletable == null) { return(false); } DataRule olddr = TableUtil.GetDataRule(table); if (olddr != null) { table.TableNewRow -= new DataTableNewRowEventHandler(olddr.OnTableNewRow); table.ColumnChanged -= new DataColumnChangeEventHandler(olddr.OnColumnChanged); } table.TableNewRow += new DataTableNewRowEventHandler(dr.OnTableNewRow); table.ColumnChanged += new DataColumnChangeEventHandler(dr.OnColumnChanged); table.TableName = dr.RuleName; TableUtil.SetProperty(table, SysConstant.scDataRule, dr); TableUtil.SetProperty(table, SysConstant.scBindAlias, alias); TableUtil.SetProperty(table, SysConstant.scPageManager, new PageManager(table)); DataColumnCollection columns = table.Columns; DataColumn column; RuleColumn rulecolumn; DataRowCollection rows = ruletable.Rows; if (columns.Count > 0) { if (table.Rows.Count > 0) { table.Rows.Clear(); } columns.Clear(); } for (int i = 0; i < rows.Count; i++) { rulecolumn = RuleColumn.FromDataRow(rows[i]); column = new DataColumn(rulecolumn.ColumnName); TableUtil.SetProperty(column, SysConstant.scRuleColumn, rulecolumn); column.Caption = rulecolumn.Label; column.DataType = DataTypes.ToType(rulecolumn.DataType); if (rulecolumn.DataType == DataTypes.dtString) { column.MaxLength = rulecolumn.Size; } if (rulecolumn.HasDefValue) { column.AllowDBNull = rulecolumn.IsNullable; } columns.Add(column); } return(true); } catch (Exception e) { throw new Exception("SDP-ST02 填充元数据出错:" + e.Message); } }