nav
nav

Reputation: 127

Programmatically created WPF DataGrid Columns not Binding

My Xaml looks like this:

<DataGrid Name="gridBasket" AutoGenerateColumns="False">
   <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding BasketName}">
      </DataGridTextColumn>
   </DataGrid.Columns>
</DataGrid>

And my class looks like:

public class Fruit
{
    public int FruitId { get; set; }
    public string FruitName { get; set; }
    public int FruitCount { get; set; }
 }

public class Basket
{
    public int BasketId { get; set; }
    public string BasketName { get; set; }
    public ObservableCollection<Fruit> Fruits { get; set; }
}

public class Shop
{
    public static ObservableCollection<Basket> Bind()
    {
        return new ObservableCollection<Basket>
        {
            new Basket { BasketId = 1, BasketName = "Gold",
                Fruits = new ObservableCollection<Fruit>
                {
                    new Fruit { FruitId = 1, FruitName = "Oranges", FruitCount = 10 },
                    new Fruit { FruitId = 2, FruitName = "Apples", FruitCount = 8 },
                    new Fruit { FruitId = 3, FruitName = "Bananas", FruitCount = 6 }
                }
            },
            new Basket { BasketId = 2, BasketName = "Silver",
                Fruits = new ObservableCollection<Fruit>
                {
                    new Fruit { FruitId = 1, FruitName = "Oranges", FruitCount = 5 },
                    new Fruit { FruitId = 2, FruitName = "Apples", FruitCount = 4 },
                    new Fruit { FruitId = 3, FruitName = "Bananas", FruitCount = 3 }
                }
            }
        };
    }
}

In my code behind I have tried to add columns dynamically and set the bindings of these dynamic columns to the nested collection property.

private void LoadData()
{
    gridBasket.AutoGenerateColumns = false;

    ObservableCollection<DemoBEL.Basket> bColl = DemoBEL.Shop.Bind();
    gridBasket.ItemsSource = bColl;

    int i = 0;
    foreach (DemoBEL.Fruit fObj in bColl[0].Fruits)
    {
        gridBasket.Columns.Add(AddColumn(i, fObj.FruitName));
        i++;
    }
}

private DataGridTextColumn AddColumn(int i, string propName)
{
    DataGridTextColumn tc = new DataGridTextColumn();
    tc.Header = propName;
    String binding = String.Format("{{Binding Path=Fruits[{0}].{1}}}", 0, "FruitCount");
    Binding tcBinding = new Binding(binding);
    tc.Binding = tcBinding;
    return tc;
}

The Count is coming empty. But if I put same binding with fixed values, the count start appearing. What changes should I make such that the count for each fruit start appearing in the data grid under the designated fruit in the header.

Cells are blank

Upvotes: 1

Views: 1235

Answers (1)

Suresh
Suresh

Reputation: 4149

Assuming you have already set the right DataContext on your view, the path you are using in your Binding constructor is incorrect. You just need to pass the property name/path to the Binding constructor. You do not need to pass the formatted string with {Binding} and all as you are doing in your AddColumn method.

Update your AddColumn method to the following:

private DataGridTextColumn AddColumn(int i, string propName)
{
    DataGridTextColumn tc = new DataGridTextColumn();
    tc.Header = propName;
    Binding tcBinding = new Binding(string.Format("Fruits[{0}].FruitCount", i));
    tc.Binding = tcBinding;
    return tc;
}

Refer to these MSDN pages on Binding and Data Binding Overview to better understand DataBinding.

Here is the output after making change to the AddColumn method:

enter image description here

Upvotes: 2

Related Questions