WinForm devexpress做主從表
dev GridLevelTree 官方檔案
dev GridLevelNode 官方檔案
感興趣可以先看看 反正我覺得講的不是人話…
網上其實很多例子 講的比較好的 比如這個 代碼清晰 邏輯不算復雜 DEV—【GridControl主從表】
但是目前有個需求 我的從表里面還有一個List 但是我不想要那個List顯示出來 正常寫法的話 他會自動幫我加載
網上也沒找到 然后看基類發現一個屬性 說人話就是你是不是只想顯示自己設定的關系 不是的話 他自動給你加上去 默認false
//
// 摘要:
// Gets or sets whether the Grid Control displays only the relationships that exist
// in the DevExpress.XtraGrid.GridControl.LevelTree.
[Browsable(true)]
[DefaultValue(false)]
[DevExpressXtraGridLocalizedDescriptionAttribute("GridControlShowOnlyPredefinedDetails")]
[DXCategory("Behavior")]
public virtual bool ShowOnlyPredefinedDetails { get; set; }
所以完成需求 改這個屬性就完事兒了
最后貼一段比較完整的代碼
注意當前的代碼是在GridControl的一個封裝里面寫的
this指向GridControl本身
//只顯示已設定的關系 默認false
this.ShowOnlyPredefinedDetails = true;
//創建一個從表的GridView物件
var childView = new GridView();
childView.ViewCaption = "好看名字";//顯示表格標題用的
childView.Name = "childView";
childView.GridControl = this;
//巴拉巴拉的一些設定 根據需求來
childView.OptionsBehavior.Editable = false;
childView.OptionsView.ShowGroupPanel = false;
childView.OptionsView.ColumnAutoWidth = false;
//構建GridLevelNode并添加到LevelTree集合里面
var node = new GridLevelNode();
node.LevelTemplate = this.ChildView;
node.RelationName = "關系名稱";// 關系名稱 如果是物件 就填物件名稱 如 Items 是表 就填表關系欄位
this.LevelTree.Nodes.AddRange(new GridLevelNode[] { node });
//添加對應的視圖集合顯示
this.ViewCollection.Add(this.ChildView);
//創建從表顯示的列
childView.Columns.Clear();
List<string> ChildViewSortColumnList=new List<string>();// 要顯示的列 欄位名丟進去
Dictionary<string, int> ChildViewCustomWidthColumnList=new Dictionary<string, int>();// 要設定的列寬 欄位名和寬度丟進去
GridColumn gridColumn;
int columnIndex = 0;
foreach (var item in ChildViewSortColumnList)
{
gridColumn = new GridColumn()
{
FieldName = item,
Caption = item,
VisibleIndex = columnIndex++
};
childView.Columns.Add(gridColumn);
}
ChildViewCustomWidthColumnList?.ForEach(x =>
{
var column = childView.Columns.ColumnByFieldName(x.Key);
if (column != null)
{
column.Width = x.Value;
}
});
// 最后系結上資料源
this.DataSource = dataSource;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/255953.html
標籤:其他
上一篇:powershell中運行python沒結果,輸入沒毛病,也指向path了,但就是出不來啊
下一篇:[C++] map集合的使用
