retide
retide

Reputation: 1190

how to bind members of one object to datagrid

Here is my problem. I have a class game, and i would like to bind some of the members to the datagrid, not entire object. What is better way to do it? Create another object to hold infomation, or create a datatable?

    public class Game
    {
        public string Name {get;set; }
        public string Description {get;set;}
        public string FullPath {get;set;}

    }

    List<Game> Games = new Games { game1, game2, game3 };

dataGrid1.ItemsSource = Game;  // I don't want to do this it will bind entire object.

Upvotes: 1

Views: 1477

Answers (2)

AD.Net
AD.Net

Reputation: 13399

You can still bind the whole object, turn off automatic generation of columns and create and bind columns manually.

Upvotes: 5

Tom
Tom

Reputation: 1057

Why don't you create another class that inherits the game class. Game2 for example. Then create the constructor to only set what you want/need from game one. Using linq you can filter this out.

var newData = from item in game
              select new game 2
              {
                  object1 = item.object1,
                  object2 = item.object2
              };

Then you can set newData as the datasource. Forgive me for any syntax errors. If you look up linq this is a pretty easy concept. You don't need to even inherit the game class. You can create a independent class called game 2 with the info you need.

Upvotes: 0

Related Questions