Example #1
0
        protected override void OnBindDataSource()
        {
            try
            {
                EasyQuery.LogMessage2("{0} - EasyDetailsGrid.OnBindDataSource starts...", TableName);
                this.DataSource = null;
                if (QueryDef.DataStorage != null && QueryDef.DataStorage.Tables.Count > 0 &&
                    _master != null && _master.DataStorage != null)
                {
                    if (QueryDef.DataStorage.Tables[TableName] != null && _master.DataStorage.Tables[_master.TableName] != null)
                    {
                        if (string.IsNullOrEmpty(_relname))
                        {
                            _relname = string.Format(System.Globalization.CultureInfo.InvariantCulture, "rel{0}", (UInt32)(Guid.NewGuid().GetHashCode()));
                        }
                        bool bExist = false;
                        foreach (DataRelation d0 in _master.DataStorage.Relations)
                        {
                            if (string.CompareOrdinal(d0.RelationName, _relname) == 0)
                            {
                                bExist = true;
                                break;
                            }
                        }
                        if (!bExist)
                        {
                            DataColumn[] masterColumns = _master.GetColumnsByNames(MasterKeyColumns);
                            DataColumn[] detailColumns = this.GetColumnsByNames(DetailsKeyColumns);
                            if (masterColumns.Length == 0 || detailColumns.Length == 0)
                            {
                            }
                            else
                            {
                                for (int i = 0; i < masterColumns.Length; i++)
                                {
                                    masterColumns[i].ReadOnly = true;
                                }
                                for (int i = 0; i < detailColumns.Length; i++)
                                {
                                    detailColumns[i].ReadOnly = true;
                                }
                                DataRelation dr = new DataRelation(_relname, masterColumns, detailColumns);
                                foreach (DataRelation dr0 in _master.DataStorage.Relations)
                                {
                                    if (string.Compare(_relname, dr0.RelationName, StringComparison.Ordinal) == 0)
                                    {
                                        _master.DataStorage.Relations.Remove(dr0);
                                        break;
                                    }
                                }
                                _master.DataStorage.EnforceConstraints = false;
                                _master.DataStorage.Relations.Add(dr);
                                bExist = true;
                            }
                        }
                        if (bExist)
                        {
                            BindSource.DataSource = _master.MasterSource;
                            BindSource.DataMember = _relname;
                            BindSource.AllowNew   = !ReadOnly;
                        }
                        else
                        {
                            DataTable tbl = QueryDef.DataStorage.Tables[TableName];
                            if (tbl == null)
                            {
                                base.Query();
                                tbl = QueryDef.DataStorage.Tables[TableName];
                            }
                            if (tbl != null)
                            {
                                BindSource.DataSource = _master.DataStorage;
                                BindSource.DataMember = TableName;
                            }
                        }

                        base.DataSource = BindSource;
                        if (BindingContext != null)
                        {
                            BindingManagerBase bm = BindingContext[BindSource];
                            if (bm != null)
                            {
                                QueryDef.SetBindingContext(bm);
                            }
                        }
                        OnFillData();
                    }
                }
            }
            catch (Exception e)
            {
                FormLog.NotifyException(ShowErrorMessage, e, "{0} - EasyDetailsGrid.OnBindDataSource", TableName);
            }
            EasyQuery.LogMessage2("{0} - EasyDetailsGrid.OnBindDataSource ends---", TableName);
        }