I've got a XamDataGrid where I'm adding columns dynamically. The columns add themselves directly, and the headers are correct, but the data cell values are blank. I can't figure out how to get the binding right.
I've read through quite a few other similar threads, but none of them seemed to fit my situation right.
Code snippets are below. I tried to cut out superfluous detail; apologies for any artifacts from those edits.
The interesting part is the AddColumn() method at the end. I first tried using a Field type instead of the UnboundField, but I couldn't find an appropriate method or property to set binding. Following an example in another thread, I tried the UnboundField as you can see below, but it's not right. (I hope it's not something silly like an incomplete BindingPath, but it's possible.)
Any help would be much appreciated.
WPF Code:
<igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:FieldLayout.Fields /> </igDP:FieldLayout></igDP:XamDataGrid.FieldLayouts>
Code-Behind:
// storage class for each rowpublic class QBRow{ public List<string> Items { get; private set; } public QBRow(string rowHeader) { Items = new List<string>(); }}// ctor for the window that contains the XamDataGridpublic QuotesBoard(){ Rows = new BindingList<QBRow>(); InitializeComponent(); grid.DataSource = Rows; //grid is the XamDataGrid, of course} // method that adds the columnprivate void AddColumn(string r0, string r1, string r2){ Rows[0].Items.Add(r1); Rows[1].Items.Add(r2); Rows[2].Items.Add(r3); int index = Rows[0].Items.Count - 1; Infragistics.Windows.DataPresenter.UnboundField f = new Infragistics.Windows.DataPresenter.UnboundField(); f.Name = "newCol_"+index.ToString(); f.DataType = typeof(string); f.BindingPath = new PropertyPath("Items["+index+"].Value"); //Also attempted: "Items[0].Value" grid.FieldLayouts[0].Fields.Add(f);}
gbirchmeier said: Rows[0].Items.Add(r1); Rows[1].Items.Add(r2); Rows[2].Items.Add(r3); int index = Rows.Items.Count - 1; Infragistics.Windows.DataPresenter.UnboundField f = new Infragistics.Windows.DataPresenter.UnboundField(); f.Name = "newCol_"+index.ToString(); f.DataType = typeof(string); f.BindingPath = new PropertyPath("Items["+index+"].Value"); //Also attempted: "Items[0].Value" grid.FieldLayouts[0].Fields.Add(f);
I could be mistaken but it looks like the issue is the value of the "index" variable. You are using the # of Rows (i.e. the number of data items) and not the # of items in the Items collection of each row. That being said if each Row has a different # of items then you really won't be able to do this using an Unbound field with a Binding(Path). The only way for that then would be to handle the InitializeRecord and set the cell's value to whatever you want to show. Alternatively if for example you wanted to show the last item in the Items collection then you might expose another property on your QBRow (e.g. "LastRow") and you send the property change notification when the collection changes (and therefore the value fot he LastRow has changed).
The index was a typo introduced when I was cleaning up the example for posting.
The line should indeed be: "int index = Rows[0].Items.Count -1;". I've fixed it in the top-post.
In this particular case, all rows will have the same number of items.
Basically, I'm trying to turn the grid sideways: the number of rows is constant, but columns can be added/removed. Is there a better way to go about this?