devExpress grid:父級grid與子grid關聯時可能會出現錯誤:不能啟用此約束,因為不是所有的值都具有相應的父值。

來源:互聯網
上載者:User

1,devExpress grid:父級grid與子grid關聯時可能會出現錯誤:不能啟用此約束,因為不是所有的值都具有相應的父值。

  解決方案:

       dsMain.Relations.Add("套播資訊", dsMain.Tables[0].Columns["DaypartID"], dsMain.Tables[1].Columns["DaypartID"],false);//false 表示不進行父子約束

  其中 dsMain 為dataset, tables[0]為父表的資料來源,tables[1]為從表的資料來源

 

2,顯示子級grid只能通過代碼的方式進行:

 

        #region ###左側帶加號        //明細顯示效果設定        private void SetDetailColumns(DevExpress.XtraGrid.GridControl gridCtrl,DevExpress.XtraGrid.Views.Grid.GridView ViewTemp, string colName, string Caption, bool isVisible            , bool isAllowEdit, string alignment, int width = 75            , DevExpress.Utils.FormatType formatType = DevExpress.Utils.FormatType.None            , string formatString = "", string ControlType = "txt")        {            ViewTemp.Columns[colName].Caption = Caption;            ViewTemp.Columns[colName].Visible = isVisible;            ViewTemp.Columns[colName].OptionsColumn.AllowEdit = isAllowEdit;            ViewTemp.Columns[colName].Width = width;            ViewTemp.Columns[colName].DisplayFormat.FormatType = formatType;            ViewTemp.Columns[colName].DisplayFormat.FormatString = formatString;            //列對齊            if (alignment != "")            {                ViewTemp.Columns[colName].AppearanceCell.TextOptions.HAlignment = HorzAlignmentByString(alignment);            }            ViewTemp.Columns[colName].AppearanceHeader.TextOptions.HAlignment = HorzAlignmentByString("Center");            //參照SOET:SOET_SYS\ControlSet\XtraGrid函數:GV_CreateColumn            //DevExpress.XtraEditors.Repository.RepositoryItemTextEdit riteEdit;            //DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit ricrEdit;            //列控制項初始化            switch (ControlType)            {                case "txt":                    //riteEdit = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();                    //grdPublish.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] { riteEdit });                    //ViewTemp.Columns[colName].ColumnEdit = riteEdit;                    break;                case "chk":                    DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit ricrEdit;                    ricrEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();                    ricrEdit.ValueChecked = "1";                    ricrEdit.ValueUnchecked = "0";                    gridCtrl.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] { ricrEdit });                    ViewTemp.Columns[colName].ColumnEdit = ricrEdit;                    break;            }        }        private static DevExpress.Utils.HorzAlignment HorzAlignmentByString(string s)        {            if (s == "") return DevExpress.Utils.HorzAlignment.Default;            return (DevExpress.Utils.HorzAlignment)Enum.Parse(typeof(DevExpress.Utils.HorzAlignment), s);        }        private void bandedGridView1_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)        {             this.Cursor = Cursors.WaitCursor;            try            {                //bandedGridView1.ClearSelection();                detailView1 = null;                detailView1 = this.bandedGridView1.GetDetailView(e.RowHandle, e.RelationIndex) as DevExpress.XtraGrid.Views.Grid.GridView;                detailView1.SelectionChanged +=new DevExpress.Data.SelectionChangedEventHandler(detailView1_SelectionChanged);                DevExpress.XtraGrid.Views.BandedGrid.BandedGridView tmpView=(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)detailView1;                //只留一個band                for (int i = tmpView.Bands.Count;i>1 ; i--)                {                    tmpView.Bands.RemoveAt(1);                }                tmpView.OptionsView.ShowBands = false;                 SetDetailColumns(grdMain, detailView1, "DaypartID", "時段代號", false, false, "Near");//隱藏列                SetDetailColumns(grdMain, detailView1, "Item", "序號", true, false, "Near");                SetDetailColumns(grdMain, detailView1, "MediaID", "媒體編號", false, false, "Near");//隱藏列                SetDetailColumns(grdMain, detailView1, "MediaName", "媒體名稱", true, false, "Near",300);//300                SetDetailColumns(grdMain, detailView1, "StartTime", "開始時間", true, false, "Near");                SetDetailColumns(grdMain, detailView1, "EndTime", "結束時間", true, false, "Near");                SetDetailColumns(grdMain, detailView1, "Dayofweek", "播出星期", true, false, "Near",120);//120                SetDetailColumns(grdMain, detailView1, "ProgramName", "節目名稱", true, false, "Near",400);//400                SetDetailColumns(grdMain, detailView1, "Position", "時段位置", true, false, "Near",200);//200                SetDetailColumns(grdMain, detailView1, "Rating", "收視率", true, false, "Near");              }            catch (Exception ex)            {                XtraMessageBox.Show(ex.Message, this.Text);            }            finally            {                this.Cursor = Cursors.Default;            }        }        #endregion ###左側帶加號        #region ###每次只讓一個grid選中        private void bandedGridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)        {            if (detailView1 != null && detailView1.GetSelectedRows().Length > 0)            {                detailView1.ClearSelection();            }        }         private void detailView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)        {            bandedGridView1.ClearSelection();        }        #endregion ###每次只讓一個grid選中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.