1

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

nav
  • 127
  • 1
  • 2
  • 10
  • Why don't you want to use binding? Dynamically created columns and data binding are not mutually exclusive concepts. – Mark Feldman Jan 17 '16 at 10:44
  • @MarkFeldman I am willing to use anything as long as I get the desired output from the class provided. I just need a push in the right direction. – nav Jan 17 '16 at 11:05
  • there's a good SO answer showing how to do it [here](http://stackoverflow.com/questions/320089/how-do-i-bind-a-wpf-datagrid-to-a-variable-number-of-columns), there's also an article about it [here](http://www.codeproject.com/Tips/676530/How-to-Add-Columns-to-a-DataGri) which uses a similar technique. – Mark Feldman Jan 17 '16 at 11:11

1 Answers1

2

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

Suresh
  • 4,091
  • 28
  • 35