如何在运行时绑定advBandedGridView?

时间:2013-01-28 11:38:12

标签: c# winforms devexpress master-detail xtragrid

我是使用DevExpress的新手。我需要设计并绑定一个复杂的DataGrid。

我是使用Designer设计的。数据网格类型为Master-Detail,它包含“MainGrid”和其他详细网格。其中一个是类型:'advBandedGridView'

MainGrid的设计如下所示:

enter image description here

'advBandedGridView'的设计如下:

enter image description here

现在,我需要使用Lists集合填充我的DataGrid,因此我使用了以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ArrayList a = new ArrayList();
            Term_Space_Grid t = new Term_Space_Grid("x", "y", true, "z");
            t.expansions = new List<MyExpansions>();
            t.expansions.Add(new MyExpansions(0, "Aya", 0, 0, 0, 0, 0));
            a.Add(t);
            resultsGridControl.DataSource = a;


        }
    }

    public class Term_Space_Grid
    {
        public string x { get; set; }
        public string y { get; set; }
        public string g { get; set; }
        public bool z { get; set; }
        public List<MyExpansions> expansions { get; set; }

        public Term_Space_Grid(string x, string y, bool z, string g)
        {
            this.x = x;
            this.y = y;
            this.z = z;
            this.g = g;

        }
    }
    public class MyExpansions
    {
        public Morphos morphos { get; set; }
        public Semantics semantics { get; set; }  

        public MyExpansions(int morphoID, string morphoDerivation, int synID, int subID, int supID, int hasID, int insID)
        {
            this.morphos = new Morphos(morphoID, morphoDerivation);
            this.semantics = new Semantics(synID, subID, supID, hasID, insID);

        }

    }

    public class Morphos
    {
         //public List<Morph> morph{ get; set; }
        public Morph morph { get; set; }

         public Morphos(int morphoID, string morphoDerivation)
        {

            //this.morph = new List<Morph>();
            //this.morph.Add(new Morph(morphoID, morphoDerivation));
            this.morph = new Morph(morphoID, morphoDerivation);
         }
    }      

    public class Semantics
    {
        public List<Sem> synonyms { get; set; }
        public List<Sem> subClasses { get; set; }
        public List<Sem> superClasses { get; set; }
        public List<Sem> hasInstances { get; set; }
        public List<Sem> instanceOf { get; set; }

        public Semantics(int id1,int id2, int id3, int id4, int id5 )
        {
            this.synonyms = new List<Sem>();
            this.subClasses = new List<Sem>();
            this.superClasses = new List<Sem>();
            this.hasInstances = new List<Sem>();
            this.instanceOf = new List<Sem>();

            this.synonyms.Add(new Sem(id1));
            this.subClasses.Add(new Sem(id2));
            this.superClasses.Add(new Sem(id3));
            this.hasInstances.Add(new Sem(id4));
            this.instanceOf.Add(new Sem(id5));
        }
    }

     public class Morph
    {
         public int MorphoID { get; set; }
        public string MorphoDerivation { get; set; }

         public Morph(int morphoID, string morphoDerivation)
        {
            this.MorphoID = morphoID;
            this.MorphoDerivation = morphoDerivation;
         }
    }
    public class Sem
    {
        public int SemID { get; set; }
        //public string MorphoDerivation { get; set; }

        public Sem(int semID)
        {
            this.SemID = semID;

        }
    }
}

但是,我发现结果是作为一个没有任何设计形式的新DataGrid构建的。我的意思是我在Designer中定义的细节选项卡没有出现在结果网格中。

结果如下:

enter image description here

注释

  1. 结果网格的设计与我的设计完全不同,我认为它就像Lists对象。
  2. 另一个问题是外观: 网格单元格中的“WindowsFormsApplication2.Morphos”和“WindowsFormsApplication2.Semantics”,而不是我传递的值!

1 个答案:

答案 0 :(得分:1)

首先,您应该通过GridColumn.FildName属性在数据对象属性和GridView列之间创建关联。
对于您的主视图(gridView2),它看起来像这样:

// gridColumn1
this.gridColumn1.Caption = "ID";
this.gridColumn1.FieldName = "x"; // associate this column with Term_Space_Grid.x property
this.gridColumn1.Name = "gridColumn1";

请阅读以下文章了解更多详情:Creating Columns and Binding Them to Data Fields

其次,您不能直接将列绑定到对象的嵌套属性(例如,MyExpansions.semantics.subclasses.SemID)。

要绕过此限制,您可以使用以下几种方法:

  1. 最简单的方法是使用Unbound Columns和相应的ColumnView.CustomUnboundColumnData事件(您可以处理此事件以提供嵌套对象的数据)。
  2. 您还可以使用以下知识库文章中展示的方法:How to display and edit complex data properties in grid columns
  3. 第三,要获得正式且有保证的答案,您应该直接向DevExpress Support Center解决与任何DevExpress产品相关的紧急问题。