Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1585
Binding to Collection
posted

I am trying to make a very flexible binding situation for my XamGrid.Basically I want to make the Grid always dynamic. The amount of columns can vary depedning on the case and their Keys can change so I would like to dynamically build it all the time. It's not Hierarchial data, its a flat grid. So I just have a collection of rows that contain a collections of cells.

I want to have it two way bind to a collection at runtime. So based upon my collection I will build the the corresponding columns and bind to my rows collection.

Here's some code...

Class Row that contains the List of Cells for the respective Row

public class Row{

   public Row()
{
Cells = new List<Cell>();
}

public List<Cell> Cells { getset; }
}

Class Cell that contains the data for the individual cell

    public class Cell
    {
        public string Value { getset; }
    }

Initialize a test collection, create column datatemplates then add to columns to XamGrid, and set the XamGrid item source

           
int numOfColumns = 3;
string[] keys = new string[] { "Key1""Key2""Key3" };

List<Row> Rows = new List<Row>();

for (int i = 0; i < 10; i++)
{
Row r = new Row();
  for (int j = 0; j < numOfColumns; j++)
   {
  Cell c = new Cell();
     c.Value = String.Format("Row {0} Cell {1}", i, j);
     r.Cells.Add(c);
   }
  Rows.Add(r);
}

for (int i = 0; i < numOfColumns; i++)
{
TemplateColumn column = new TemplateColumn();
   column.ItemTemplate = Create(typeof(TextBox),
String.Format("Cells[{0}].Value", i));
   
column.Key = keys[i];
   XamGrid.Columns.Add(column);
}

XamGrid.ItemsSource = Rows;



Here's the DataTemplate Create method I use



public static DataTemplate Create(Type type, string path)
{
return
 (DataTemplate)XamlReader.Load(
@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">
<"
 + type.Name + @" Text=""{Binding Path= "+ path + @", Mode=TwoWay}""/>
</DataTemplate>"
);
}

When I run this I get the following Error:

Unhandled Exceprion

Message: Infragistics.Controls.Grids.InvalidColumnKeyException: The following key(s) do not correspon with the DataSource: "Key1" If you'd like to add additional columns, please use the unbound column type....

I want to ensure I have TwoWay binding, so I definitely want to make sure my columns are binding in that mode.

So instead of TemplateColumn I change it to UnboundColumn and I no longer get the error,

 for (int i = 0; i < numOfColumns; i++)
 {
  UnboundColumn column = new UnboundColumn();
    //TemplateColumn column = new TemplateColumn();
    column.ItemTemplate = Create(typeof(TextBox),
String
.Format("Cells[{0}].Value", i));
   
    column.Key = keys[i];
    XamGrid.Columns.Add(column);
}

 

 

but Its displaying as hierarchical and I want a flat grid....

 

What am I doing wrong ?