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选中