locked
Binding the datagrid to a list of objects RRS feed

  • Question

  • Hi,

    I currently have a datagrid (from the WPF toolkit) which I would like to link to a list of objects.

    I was already able to do it dinamicaly:

    datagrid.ItemsSource = ListI;

    The problem is If the list is empty I can't see a new line in the datagrid...

    So far what I've done is added an item to the list and then remove it...

    is there another way to do it?

    also, Can the data binding be done on XAML?

    Thanks in advance
    Wednesday, February 18, 2009 5:43 PM

Answers

  • It could be a property of a different datasource. 

    For example...

    I have a class which has a function in it that retrieves a list of users from a database using a data adapter:


            public DataView GetListOfUsers()
            {
                My_v3 users = new My_v3();
                GetUsersTableAdapter adapter = new GetUsersTableAdapter();
                adapter.Fill(users.GetUsers);
                return users.GetUsers.DefaultView;
            }

    Then in my constructor of the UserControl that holds the grid I do this:


    public MainGrid()
            {
                InitializeComponent();

                LoadUsers();
            }

    Which calls this function that loads the users into my grid:

    public void LoadUsers()
            {
                //Create a new datacontroller and get the list of patients
                GridPrototype.Controllers.DataController data = new GridPrototype.Controllers.DataController();
                userGrid.ItemsSource = data.GetListOfUsers();
            }

    In my xaml for the Grid I have this:

    <my:DataGrid Name="userGrid" ItemsSource="{Binding}" AutoGenerateColumns="True">

    In one of my DataGridTemplateColumn.CellTemplate I have this binding to retreive the AGE field from the table returned by the dataset:

    <TextBlock Text="{Binding GENDER}" Margin="8,0,0,0"/>


    • Edited by DeviantSeev Friday, February 20, 2009 4:53 PM changed code
    • Proposed as answer by DeviantSeev Friday, February 20, 2009 6:19 PM
    • Marked as answer by RuiLopes Friday, February 27, 2009 11:59 AM
    Friday, February 20, 2009 4:52 PM

All replies

  • You can do a binding in XAML as follows:

    ItemsSource="{Some Binding Code}"

    As far as having the grid automatically start out with a new row, have you tried:

    CanUserAddRows="True"
    Wednesday, February 18, 2009 7:12 PM
  • Hi,

    Sorry for taking this long to answer... I couldn't have access to internet..

    What should I put in "{Some binding code}"??

    I would like to link it to a private list<int> for example..

    how should I build the binding code?

    • Marked as answer by RuiLopes Friday, February 20, 2009 5:16 PM
    • Unmarked as answer by RuiLopes Friday, February 27, 2009 11:59 AM
    Friday, February 20, 2009 3:53 PM
  • The "{Some Binding Code}" will depend on what property or data source you're binding to.

    For example if I have a List<int> I can create the following property in my code-behind:


    public List<int> Age
    {
    get;
    set;
    }

    then I can try binding like this:

    ItemSource="{Binding Age}"
    Friday, February 20, 2009 4:07 PM
  • Thanks for the input,

    I've tried it but, when I run the program, the datagrid doesn't appear connected to the list..

    Is there any code missing?

    does it have to be a property?

    thanks in advance
    Friday, February 20, 2009 4:35 PM
  • It could be a property of a different datasource. 

    For example...

    I have a class which has a function in it that retrieves a list of users from a database using a data adapter:


            public DataView GetListOfUsers()
            {
                My_v3 users = new My_v3();
                GetUsersTableAdapter adapter = new GetUsersTableAdapter();
                adapter.Fill(users.GetUsers);
                return users.GetUsers.DefaultView;
            }

    Then in my constructor of the UserControl that holds the grid I do this:


    public MainGrid()
            {
                InitializeComponent();

                LoadUsers();
            }

    Which calls this function that loads the users into my grid:

    public void LoadUsers()
            {
                //Create a new datacontroller and get the list of patients
                GridPrototype.Controllers.DataController data = new GridPrototype.Controllers.DataController();
                userGrid.ItemsSource = data.GetListOfUsers();
            }

    In my xaml for the Grid I have this:

    <my:DataGrid Name="userGrid" ItemsSource="{Binding}" AutoGenerateColumns="True">

    In one of my DataGridTemplateColumn.CellTemplate I have this binding to retreive the AGE field from the table returned by the dataset:

    <TextBlock Text="{Binding GENDER}" Margin="8,0,0,0"/>


    • Edited by DeviantSeev Friday, February 20, 2009 4:53 PM changed code
    • Proposed as answer by DeviantSeev Friday, February 20, 2009 6:19 PM
    • Marked as answer by RuiLopes Friday, February 27, 2009 11:59 AM
    Friday, February 20, 2009 4:52 PM