Good afternoon.
Give, please, a code example, how to realize hierarchical datasource as a collection.
I attach a project that should work according to the instructions, but don't work correctly.
Hi,
I ran your sample project and it works just fine for me. My guess is that you just need to get the latest service release.
How to get the latest service release - Infragistics Community
In any case, I don't recommend using List<T> or binding the grid to an array like you are doing here. If you bind the grid to an Array or a List<T>, then you are using the IList interface, which is not a robust interface for data binding. You will have limited functionality in the grid if you use ILists.
I recommend using BindingList<T>, instead.
private void Form1_Load(object sender, EventArgs e) { A a = new A {Id = 1}; B b = new B {Name = "b1", ParId = 1}; a.Coll = new BindingList<B>{b}; ultraGrid1.DataSource = new BindingList<A> { a }; B b2 =new B{Name = "b2", ParId = 1}; B b3 = new B { Name = "b3", ParId = 1 }; a.Coll.Add(b2); a.Coll.Add(b3); } public class B { public int ParId { get; set; } public string Name { get; set; } } public class A { public int Id { get; set; } public BindingList<B> Coll { get; set; } }
If you do that, it might work without the service release.
Mike,
thanks for the quick reply.
My problem is: if I add a field to the "B" class, then an excess column appears in the grid, though I don't want it to appear. Changing List to BindingList doesn't help here.
How to improve the situation?
I'm not sure what you are asking. What do you mean by "an excess column?" The grid will show all of the fields that exist in the DataSource you bind to. If you don't want to show a particular field in the grid, then you have a number of options.
You could choose not to include that field in your DataSource.
You could mark that field Browsable[false].
Or you could simply hide the grid column using the hidden property.
Thanks, the situation became almost clear for me. The browsable attribute works, hidden property works, too.
However, setting hidden property is inconvenient, because of a new field may appear in any time by any developer that isn't even aware that a class is used as datasource. For the same reason that developer may forget to mark Browsable attribute.
It is interesting how to make it work using not including the field in my DataSource.
Let in my example class B have an additional property
public int F{get; set; }
That property should not be included into datasource.
I can suggest a correct, but hard way - to define a class D that contains only needed fields and define a converter B into D and use D as datasource. Also, one can create a datasource object with rows (the datasource is already a table, not collection). But isn't there a more simple solution?
It sounds like what you want is the NewColumnLoadStyle property:
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { UltraGridLayout layout = e.Layout; layout.NewColumnLoadStyle = NewColumnLoadStyle.Hide; }
Another option would be to loop through all of the columns and set Hidden to true on all of the columns that you do not explicitly want to be visible.
I tried NewColumnLoadStyle property, but it didn't work for me: no columns are shown at all. I'm attaching a modified project. Looping is a solution but not a flexible one, because there's no point to set the schema in ultrawingrid designer.
What solution is suitable for the attached project?
Thanks for reply.
Some of the given variants don't work, such as setting NewColumnLoadStyle at design-time or at run-time. Using InitializeLayout event doesn't work as well. Anyway, for answering on this thread it would be better to provide some working code here or a reference to existing resources.
I've got a decision that is a "workaround", but it works and may be useful.
1. Don't desing datagrid fields.
2. Set ultrawingrid datasource as collection.
3. Hide all the columns in a band iteratively.
4. With reflection dynamically get string names of the properties you need to show as fields.
5. Set for these properties Header.VisiblePosition, Header.Caption and Hidden=false.
The advantages of this variant are
- if any property name changes, you get a complilation error.
- If any two properties change places in class file, that will not affect the view.
- If any property is added, that will not affect the view.
- You can save and restore layout for all the bands.
I'm not sure I am following you.
The sample you attached here is not setting NewColumnLoadStyle until run-time. If your actual application is setting up the layout at design-time, then this will only affect new columns added at run-time after you already bound the grid, so that doesn't make a lot of sense.
What's the problem with setting the Hidden property of the column(s) at run-time? One way or another you have to decide which columns are visible and which are not.
If someone adds a new column to the data source, then NewColumnLoadStyle (if set at design-time) will determine if that new column is visible by default. It can only default to hidden or not hidden.
In your original post, you seemed to indicate that you wanted it hidden by default, so that whoever adds it doesn't accidentally add a column to the grid without realizing it. So the solution to that is to set NewColumnLoadStyle on the grid at design-time. Setting it at run-time won't do any good there, anyway.
The other option I presented is to use the InitializeLayout event and hide all of the columns except the ones you want to show. This will have essentially the same effect as setting NewColumnLoadStyle at design-time - any new columns added to the data source will not automatically display in the grid without someone taking some action.
Either way, the new columns won't show up unless someone takes action to make them show up.
I don't understand what else you are looking for here.