in
Forums
Blogs
Files
Devexpress.Com
Client Center
Support Center
DevExpress Channel

Datasource of gridControl

Last post 11/19/2008 8:27 AM by Marc Greiner [DX-Squad]. 1 replies.
Page 1 of 1 (2 items)
Sort Posts:
Previous Next
  • 11/19/2008 1:58 AM

    Datasource of gridControl

    How to banging objects to gridcontrol?

    For example:

    class Company
    {
        string name;
        public string Name
        {
           get {return name;}
           set {name = value;}
        }
    }

    class Group
    {
        company myCompany;
        public Compay MyCompany
        {
            get {return myCompany;}
            set {myCompany = value;}
        }
    }

    class User
    {
       string name;
       int age;
       Group myGroup;
      
       public string Name
       {
           get {return name;}
           set {name = value;}
       }

       public string Age
       {
           get {return age;}
           set {age = value;}
       }
      
       public Group MyGroup
       {
           get {return myGroup;}
           set {myGroup = value;}
       }
    }


    static mani()
    {
    //create data source
        Company company = new Company();
        company.Name = "Com";

        Group group = new Group();
        group.Name = "Test Group";

        User[ users = new User[2];

        users[0] = new User();
        users[0].Name = "Jim";
        users[0].Age = 10;
        users[0].Group = group;

        users[1] = new User();
        users[1].Name = "Tim";
        users[1].Age = 20;
        users[1].Group = group;


       // i want to display the users' informations at the same gridcontrol
       // such as: user.Name,user.age, user.MyGroup.Name, user.MyGroup.MyCompany.Name

    // i coding like this  

    DevExpress.XtraGrid.GridControl grid = CreateGridControl();
       AddGridColumn(grid);

       grid.DataSource = users;

    }

    private DevExpress.XtraGrid.GridControl CreateGridControl()
    {
                //
                DevExpress.XtraGrid.GridControl grid = new DevExpress.XtraGrid.GridControl();
                DevExpress.XtraGrid.Views.Grid.GridView view = new DevExpress.XtraGrid.Views.Grid.GridView();

                //
                //
                //
                grid.EmbeddedNavigator.Name = "";
                //grid.Location = new System.Drawing.Point(25, 24);
                grid.MainView = view;
                grid.Name = "grid";
                //grid.Size = new System.Drawing.Size(400, 200);
                grid.TabIndex = 0;
                grid.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[ {
                view});
                //
                // gridView1
                //
                view.GridControl = grid;
                view.Name = "view";

                grid.Visible = false;

                this.Controls.Add(grid);

                return grid;
    }
     
     private void AddGridColumn(DevExpress.XtraGrid.GridControl grid)
            {
                DevExpress.XtraGrid.Views.Base.ColumnView View = grid.MainView as                            

    DevExpress.XtraGrid.Views.Base.ColumnView;
                View.Columns.Clear();
                DevExpress.XtraGrid.Columns.GridColumn column;

                // user's name
                column = View.Columns.AddField("Name");
                column.Caption = "Name";
                column.OptionsFilter.AllowFilter = false;
                column.VisibleIndex = 0;
                column.OptionsColumn.AllowEdit = false;
                column.Width = 140;
                column.FieldName = "Name";

                //user's age
                column = View.Columns.AddField("Age");
                column.Caption = "Age";
                column.OptionsFilter.AllowFilter = false;
                column.VisibleIndex = 1;
                column.OptionsColumn.AllowEdit = false;
                column.Width = 140;
                column.FieldName = "Age";

                //name of user's group
                column = View.Columns.AddField("GroupName");
                column.Caption = "Group name";
                column.OptionsFilter.AllowFilter = false;
                column.VisibleIndex = 2;
                column.OptionsColumn.AllowEdit = false;
                column.Width = 140;
                column.FieldName = "MyGroup";
               
                //the company name
                //column = View.Columns.AddField("CompayName");
                //column.Caption = "Company name";
                //column.OptionsFilter.AllowFilter = false;
                //column.VisibleIndex = 3;
                //column.OptionsColumn.AllowEdit = false;
                //column.Width = 140;
                //column.FieldName = "MyGroup.Company";
               
            }


    Notic:
      if i override the "ToString" of the Group like this

    public override string ToString()
     {
             return this.Name;
     }

    the girdcontrol can display the group's name.

    but now i have no method to display the "Company.name" at the same gridcontrol by object,
    how can i do ?

     

  • 11/19/2008 8:27 AM In reply to

    Re: Datasource of gridControl

    Hi Showjan,

    Please check this forum post : "Show data of two related entities" and the responses to it with links to the knowledge base, you may find ways of doing what you want.

    Regards,
    Marc Greiner [DX-Squad]
Page 1 of 1 (2 items)
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED