我在RadGridView中创建了一个层次关系。但这种关系不起作用。我无法显示详细信息。我做错了什么?
如果我自动生成所有关系工作正常。但我只需要一个表的一个字段作为详细信息。
此外:我使用VS2013 Prof.,WinForms作为UI和LINQ to SQL作为数据源。
源代码是:
private void OpenConfigurations()
{
bsConfigurations.DataSource = Db.Configurations.Where(c => c.Active == true && c.CompanyId == CurrentCompany.CompanyId).Select(c => c).OrderBy(c => c.Name);
bsConfigurationContracts.DataSource = Db.ConfigurationContracts.Where(c => c.Contract.InsurranceId == CurrentCompany.CompanyId).Select(c =>
c).OrderBy(c => c.Contract.Name);
GridViewRelation gvrConfigurations = new GridViewRelation();
gvrConfigurations.ChildColumnNames.Add("ConfigurationId");
gvrConfigurations.ChildTemplate = this.gridViewTemplate1;
gvrConfigurations.ParentColumnNames.Add("ConfigurationId");
gvrConfigurations.ParentTemplate = this.gvConfiguration.MasterTemplate;
gvConfiguration.Relations.Add(gvrConfigurations);
}
在设计师中:
this.gridViewTemplate1.AllowAddNewRow = false;
gridViewTextBoxColumn1.EnableExpressionEditor = false;
gridViewTextBoxColumn1.FieldName = "Contract.Name";
gridViewTextBoxColumn1.HeaderText = "Name";
gridViewTextBoxColumn1.Name = "colName";
gridViewTextBoxColumn1.Width = 250;
gridViewTextBoxColumn2.EnableExpressionEditor = false;
gridViewTextBoxColumn2.FieldName = "ConfigurationId";
gridViewTextBoxColumn2.HeaderText = "ConfigurationId";
gridViewTextBoxColumn2.IsVisible = false;
gridViewTextBoxColumn2.Name = "colConfigurationId";
this.gridViewTemplate1.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] { gridViewTextBoxColumn1, gridViewTextBoxColumn2 });
this.gridViewTemplate1.DataSource = this.bsConfigurationContracts;
//
// gvConfiguration
//
this.gvConfiguration.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(233)))), ((int)(((byte)(240)))), ((int)(((byte)(249)))));
this.gvConfiguration.Cursor = System.Windows.Forms.Cursors.Default;
this.gvConfiguration.Dock = System.Windows.Forms.DockStyle.Fill;
this.gvConfiguration.Font = new System.Drawing.Font("Segoe UI", 8.25F);
this.gvConfiguration.ForeColor = System.Drawing.Color.Black;
this.gvConfiguration.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.gvConfiguration.Location = new System.Drawing.Point(0, 30);
//
//
//
this.gvConfiguration.MasterTemplate.AllowAddNewRow = false;
this.gvConfiguration.MasterTemplate.AutoGenerateColumns = false;
gridViewTextBoxColumn3.EnableExpressionEditor = false;
gridViewTextBoxColumn3.FieldName = "Name";
gridViewTextBoxColumn3.HeaderText = "Name";
gridViewTextBoxColumn3.Name = "colName";
gridViewTextBoxColumn3.SortOrder = Telerik.WinControls.UI.RadSortOrder.Ascending;
gridViewTextBoxColumn3.Width = 250;
gridViewTextBoxColumn4.EnableExpressionEditor = false;
gridViewTextBoxColumn4.FieldName = "ConfigurationId";
gridViewTextBoxColumn4.HeaderText = "ConfigurationId";
gridViewTextBoxColumn4.IsVisible = false;
gridViewTextBoxColumn4.Name = "colConfigurationId";
this.gvConfiguration.MasterTemplate.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] { gridViewTextBoxColumn3, gridViewTextBoxColumn4 });
this.gvConfiguration.MasterTemplate.DataSource = this.bsConfigurations;
sortDescriptor1.PropertyName = "colName";
this.gvConfiguration.MasterTemplate.SortDescriptors.AddRange(new Telerik.WinControls.Data.SortDescriptor[] { sortDescriptor1 });
this.gvConfiguration.MasterTemplate.Templates.AddRange(new Telerik.WinControls.UI.GridViewTemplate[] { this.gridViewTemplate1 });
this.gvConfiguration.Name = "gvConfiguration";
this.gvConfiguration.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.gvConfiguration.ShowGroupPanel = false;
this.gvConfiguration.Size = new System.Drawing.Size(1098, 72);
this.gvConfiguration.TabIndex = 4;
this.gvConfiguration.Text = "radGridView1";
this.gvConfiguration.CellDoubleClick += new Telerik.WinControls.UI.GridViewCellEventHandler(this.gvConfiguration_CellDoubleClick);
答案 0 :(得分:0)
解决方案是您必须添加列名而不是字段名称的关系。
所以这是正确的代码:
GridViewRelation gvrConfigurations = new GridViewRelation();
gvrConfigurations.ChildColumnNames.Add("colConfigurationId");
gvrConfigurations.ChildTemplate = this.gridViewTemplate1;
gvrConfigurations.ParentColumnNames.Add("colConfigurationId");
gvrConfigurations.ParentTemplate = this.gvConfiguration.MasterTemplate;
gvConfiguration.Relations.Add(gvrConfigurations);