I am working on a project to use XamGrid to create an interface that will behave like an excel worksheet in many way. User should be able to add, remove, rearrange columns at run time and persist the resultant grid structure to a database. I am binding this grid to an ObserveableCollection<TemplateRow> where
public class TemplateRow { public ObservableCollection<string> Cells { get; set;} }
Logically this object seems ok but XamGrid renders it as hierarchical and I can see why it's doing that. Any ideas on how my Grid bindings or object structure should look like to accomplish this.
Hello,
xamDataGrid can bind to any object, but while doing so it searches for child collections inside the object, like IList collections, ObservableCollections, CollectionViews, etc. At this point the grid assumes that this inner collection is a child collection and creates a hirarchy based on that.
So I guess what you need to do is just to create a base class that has all columns (fields) defined as properties, and then create an ObservableCollection based on that.
e.g.
public class Person
{
string FirstName {get;set; }
string LastName {get;set; }
}
then create ObservableCollection < Person >
and finallly bind this ObservableCollection to the grid.
HTH,
This will not work because I don't know all the fields of base class (Person class in your example) until run time. The UI behavior is more like excel where you can keep adding columns on per need basis. I can not keep adding unbound columns to the grid because eventually I have to persist this structure in a database. I added an observeablecollection<string> to accomodate this requirement. I was using my object model to bind against wpf DataGrid from Microsoft (part of their toolkit) and it was rendering correctly. But when I switched to XamGrid it started treating it as child object.
I made some progress based on previous hint. Here is what my code looks like
private void MenuItemAddTextColumn_Click(object sender, RoutedEventArgs e){ AddColumn(this, null); // delegate to presenter to add cells to observeable datasource
string colNum = templateXamGrid.FieldLayouts[0].Fields.Count.ToString(); //Play with XamGrid Layout to dynamically add field. UnboundField uf = new UnboundField(); uf.Name = "Cells[" + colNum + "]"; uf.Label = "Col " + colNum; PropertyPath p = new PropertyPath("Cells[" + colNum + "]"); uf.BindingPath = p; templateXamGrid.FieldLayouts[0].Fields.Add(uf); }
On XAML side I have set AutoGenerateFields=false. This seem to work fine. I can add columns and they all show up on same level. The problem comes if I rearrange columns in middle of adding new columns and then try to add another column. It throws an exception on following line
templateXamGrid.FieldLayouts[0].Fields.Add(uf);
The exeption says "The given key was not present in the dictionary."
Debugging through the code, I can see that AddColumn() did it's job to properly add new cell to each of exisiting rows (simulating column addition) but binding somehow is failing.
Can anyone from Infragistics help explain why grid binding is throwing this exception. This appears to be a grid problem.
I don't see the issue with the latest internal version so I suspect this has already been addressed but I would recommend submitting the issue to the support group so it can be verified that the issue you are seeing is indeed addressed and so you can be notified when a hotfix is available.
I am attaching a sample VS 2008 project using XAMGrid that replicates the issue. In order reproduce this behavior
This issue has been addressed and resolved in the next release v2009.1. It will be released in the beginning of April or the next hotfix in the mid of April
Alex.