实现效果,可以通过前面的±号,选择是否展开。
首现要将DevExpress需要显示的对象和子对象封装成类,在类中就定义为嵌套
的形式
internal class WarningDetail
{
public string Date { get; set; }
public string Description { get; set; }
}
internal class WarningInfo
{
public string ID { get; set; }
public string Date { get; set; }
public string Warning { get; set; }
public List<WarningDetail> Detail2List { get; set; }
}
然后,绑定GridView的数据,表格的宽度按照需求设定
private void TimeDataViewer(string[] list, DateTime _startTime, DateTime _endTime)
{
//为链接动作,需要将数据存储到队列并记录位置
List<WarningInfo> warningInfoList = new List<WarningInfo>();
//数据处理部分省略
//绑定数据
this.TimeGridControl.DataSource = warningInfoList;
//绑定子表
GridView grv2 = null;
//共用一个GridView和GridControl
var grv = this.TimeGridView;
var gridControl = this.TimeGridControl;
//创建一个从表的GridView对象
grv2 = new GridView();
grv2.ViewCaption = "记录明细";
grv2.Name = "grv2";
grv2.GridControl = gridControl;
//构建GridLevelNode并添加到LevelTree集合里面
var node = new GridLevelNode();
node.LevelTemplate = grv2;
node.RelationName = "Detail2List";//这里对应集合的属性名称
gridControl.LevelTree.Nodes.AddRange(new GridLevelNode[] { node });
//添加对应的视图集合显示
gridControl.ViewCollection.Clear();
gridControl.ViewCollection.AddRange(new BaseView[] { grv, grv2 });
this.TimeGridView.GroupPanelText = "异常统计(时间)";
this.TimeGridView.OptionsView.ShowGroupPanel = true;
this.TimeGridView.OptionsView.EnableAppearanceOddRow = true;//奇数行颜色变化
this.TimeGridView.Appearance.OddRow.BackColor = System.Drawing.Color.LightSteelBlue;//奇数行颜色设置
this.TimeGridView.OptionsView.EnableAppearanceEvenRow = true;//偶数行颜色变化
this.TimeGridView.Appearance.EvenRow.BackColor = System.Drawing.Color.AliceBlue;//偶数行颜色设置
this.TimeGridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;//设置只能选中整行
this.TimeGridView.OptionsBehavior.Editable = false;//设置列的数据不能编辑
表格的宽度按照需求设定,例如我设置为1:2:3 this.TimeGridView.Columns[0].Width = this.TimeGridControl.Width/6;
this.TimeGridView.Columns[1].Width = 2 * this.TimeGridControl.Width / 6;
this.TimeGridView.Columns[2].Width = 3 * this.TimeGridControl.Width / 6;
}
//允许换行显示
DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();//创建RepositoryItemMemoEdit
memoEdit.AutoHeight = true;
this.TimeGridView.Columns[1].ColumnEdit = memoEdit;//允许第二行换行显示