Esempio n. 1
0
        public static void InitDataGridViewColumns(DataGridView grid, DataTable table)
        {
            if (grid == null || table == null)
            {
                return;
            }

            //if (grid.Columns.Count > 0)
            //    grid.Columns.Clear();

            if (grid.Columns.Count == 0)
            {
                grid.EditingControlShowing -= new DataGridViewEditingControlShowingEventHandler(DataGridViewEditingControlShowing);
                grid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(DataGridViewEditingControlShowing);

                DataRule dr = TableUtil.GetDataRule(table);
                if (dr != null)
                {
                    DataColumnCollection columns = table.Columns;
                    if (columns.Count > 0)
                    {
                        DataGridViewColumn gcolumn;
                        RuleColumn         rcolumn;
                        foreach (DataColumn column in columns)
                        {
                            rcolumn = TableUtil.GetRuleColumn(column);
                            if (rcolumn != null)
                            {
                                gcolumn = CreateDataGridViewColumn(rcolumn);
                                grid.Columns.Add(gcolumn);
                            }
                            else
                            {
                                InitDataGridViewColumns(grid, dr);
                                break;
                            }
                        }
                    }
                    else
                    {
                        InitDataGridViewColumns(grid, dr);
                    }
                }
                else if (table.Columns.Count > 0)
                {
                    DataGridViewColumn gcolumn;

                    foreach (DataColumn column in table.Columns)
                    {
                        if (TableUtil.StrProperty(column, "Visible").Equals("0"))
                        {
                            continue;
                        }

                        gcolumn = null;

                        string datatype = column.DataType.ToString();

                        if (datatype.Equals("System.Boolean"))
                        {
                            gcolumn              = new DataGridViewCheckBoxColumn();
                            gcolumn.Width        = column.MaxLength > 50 ? column.MaxLength : 50;
                            gcolumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                        }
                        else if (datatype.Equals("System.DateTime"))
                        {
                            gcolumn              = new CalendarColumn("yyyy-MM-dd HH:mm:ss");
                            gcolumn.Width        = 130;
                            gcolumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                        }
                        else
                        {
                            string mapInfo = TableUtil.StrProperty(column, "MapInfo");
                            if (!mapInfo.Equals(""))
                            {
                                DataMap dm = SystemContext.MapManager.FromMapInfo(mapInfo);
                                if (dm != null)
                                {
                                    DataTable mtable = dm.GetMapTable(null);
                                    if (mtable != null && mtable.Rows.Count > 0)
                                    {
                                        gcolumn = new DataGridViewComboBoxColumn();
                                        DataGridViewComboBoxCell combboxcell = gcolumn.CellTemplate as DataGridViewComboBoxCell;
                                        if (combboxcell != null)
                                        {
                                            combboxcell.DataSource    = mtable;
                                            combboxcell.ValueMember   = "Key";
                                            combboxcell.DisplayMember = "Value";
                                        }
                                    }
                                }
                            }

                            if (gcolumn == null)
                            {
                                gcolumn              = new DataGridViewTextBoxColumn();
                                gcolumn.Width        = column.MaxLength > 100 ? column.MaxLength : 100;
                                gcolumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                            }
                        }

                        gcolumn.Name             = column.ColumnName;
                        gcolumn.HeaderText       = column.Caption;
                        gcolumn.DataPropertyName = column.ColumnName;

                        //gcolumn.Width = column.MaxLength;
                        //gcolumn.Width = column.MaxLength > 100 ? column.MaxLength : 100;

                        //gcolumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;

                        grid.Columns.Add(gcolumn);
                    }
                }

                grid.AutoGenerateColumns = false;
                grid.DataSource          = GetBindingSource(table);
            }
            else
            {
                DataColumn column;
                string     mapInfo;
                foreach (DataGridViewColumn gcolumn in grid.Columns)
                {
                    column = table.Columns[gcolumn.DataPropertyName];
                    if (column != null)
                    {
                        mapInfo = TableUtil.StrProperty(column, "MapInfo");
                        if (!mapInfo.Equals(""))
                        {
                            DataMap dm = SystemContext.MapManager.FromMapInfo(mapInfo);
                            if (dm != null)
                            {
                                DataTable mtable = dm.GetMapTable(null);
                                if (mtable != null && mtable.Rows.Count > 0)
                                {
                                    DataGridViewComboBoxCell combboxcell = gcolumn.CellTemplate as DataGridViewComboBoxCell;
                                    if (combboxcell != null)
                                    {
                                        combboxcell.DataSource    = mtable;
                                        combboxcell.ValueMember   = "Key";
                                        combboxcell.DisplayMember = "Value";
                                    }
                                }
                            }
                        }
                    }
                }

                grid.AutoGenerateColumns = false;
                grid.DataSource          = GetBindingSource(table);
            }

            grid.CellLeave -= new DataGridViewCellEventHandler(DataGridView_CellLeave);
            grid.CellLeave += new DataGridViewCellEventHandler(DataGridView_CellLeave);
        }
Esempio n. 2
0
        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);
            }
        }