Hi,
We have an ultragrid in which we bind different kinds of objects depending on an external property. We would like to have a save layout and update layout functionality through which we can save layouts for grid display for different objects in a single file. We have created a dictionary<typeOfObjectBindedToGrid, UltraGridLayout> and trying to xml-serialize it into a file.
It appears that the UltraGridLayout is not serializable and is returning error {"Infragistics.Win.UltraWinGrid.ColScrollRegion cannot be serialized because it does not have a parameterless constructor."}
Can you please let me know an elegant way to handle this keeping in view that the properties in the objects keep changing with the enhancements and the old layout file are loaded for the grids.
Thanks,
Puneet
Hi Puneet,
I'm not sure why you are getting that error message. But the grid has built-in code to save and load the layout. So it's definitely possible to do so. My best guess is that something in the way in which you are doing the serialization is causing the error. But you didn't post that code here so that's only a guess.
If you have a fixed number of potential layouts, then the easiest thing to do would be to save each layout to a stream. You could create a FileStream and then save the layouts to the stream in order. Of course, if you are binding the grid to each data source in an arbitrary order, that would be problematic. So what you could do is just create a bunch of UltraGridLayout variables: one for each possibly layout.
Every time the user switches from one layout to another, you store the current layout in the variable for that layout using layout.CopyFrom. That way you store each layout in memory. When it comes time to write the layouts to a file, you create the file stream and then save each layout in a particular order. When you want to load them back in, you load each layout from the file stream into your layout variables in the same order. This has the added advantage of having the layout already in memory - so when the user switches to a new layout, you don't have to access the file every time, you just load it from the variable.
I had implemented it in a similar way. We actually have a few other controls on the form which we would have liked to store in the user layout preferences. So what we have done is to create a separate layouts class containing variables of UltraGridLayout for our form and tried to serialize it into a file. I have attached a sample which tries to imitate the scenario. We have several grid objects in the actual case. I know about the SaveAsXml function in the UltraGridLayout class but we would not like to have multiple files and instead work with one file only.
Thanks
You can't just serialize the object like this. There's a lot more to it, which is why I strongly recommend that you use the built-in save and load methods which handle all of the binding and formatting and also versioning.
You seem to be under the impression that you cannot use SaveAsXml to save more than one layout to the same file, but that is not true. You can create a FileStream and then save all of the layouts into a single file.
There is a catch, though. an XML document cannot have more than one root node. So using SaveAsXml is problematic, unless you want to also get involved in writing a root node and then dealing with positioning to the proper position within the file in order to load the first layout.
Saving/loading to binary is easier:
private void ultraButton2_Click(object sender, EventArgs e) { MyFormLayouts myLayouts = new MyFormLayouts(); myLayouts.GridLayout1 = ultraGrid1.DisplayLayout; myLayouts.GridLayout2 = ultraGrid2.DisplayLayout; ////Following line throws error {"Cannot serialize member 'Infragistics.Win.UltraWinGrid.UltraGridLayout.ColScrollRegions' of type 'Infragistics.Win.UltraWinGrid.ColScrollRegionsCollection', see inner exception for more details."} //XmlSerializer serializeXml = new XmlSerializer(typeof(MyFormLayouts)); //XmlWriter xmlWrit= XmlWriter.Create(Application.StartupPath+"\\MyFormat.xml"); //serializeXml.Serialize(xmlWrit, myLayouts); string fileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "MyFormat.bin"); FileStream fileStream = new FileStream(fileName, FileMode.Create); myLayouts.GridLayout1.Save(fileStream); myLayouts.GridLayout2.Save(fileStream); fileStream.Flush(); fileStream.Dispose(); } private void ultraButton1_Click(object sender, EventArgs e) { string fileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "MyFormat.bin"); FileStream fileStream = new FileStream(fileName, FileMode.Open); MyFormLayouts myLayouts = new MyFormLayouts(); UltraGridLayout layout = new UltraGridLayout(); layout.Load(fileStream); myLayouts.GridLayout1 = layout; layout = new UltraGridLayout(); layout.Load(fileStream); myLayouts.GridLayout2 = layout; this.ultraGrid1.DisplayLayout.CopyFrom(myLayouts.GridLayout1); this.ultraGrid2.DisplayLayout.CopyFrom(myLayouts.GridLayout2); }