Hey,
I've a problem: I saved a gridlayout in a database as string. Then I want to load the layout again from the database. The load-function works for files and streams, so I tried to convert my string into a stream, but everytime I get the exception, that the stream has no correct binary format.
I've tried:
System.IO.TextReader tr = new StringReader(dr["layout"].ToString()); byte[] a = System.Text.Encoding.Unicode.GetBytes(tr.ReadToEnd()); System.IO.MemoryStream memStream = new System.IO.MemoryStream(a); this.ultraGrid1.DisplayLayout.Load(memStream);
and also:
System.IO.TextReader tr = new StringReader(dr["layout"].ToString()); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); StreamWriter sw = new StreamWriter(memStream); sw.Write(tr.ReadToEnd()); memStream.Seek(0, SeekOrigin.Begin); this.ultraGrid1.DisplayLayout.Load(memStream);
in both cases I get the exception.
I hope there is some help,
greetings
Mike Saltzman"] The Encoding usually works okay, but sometimes it will corrupt the data. I'm not sure exactly why, but I have seen it happen.
Using StreamReader/StreamWriter as recommended it went away.
InitializeLayout is okay. Just be aware that this event fires any time you set the DataSource or DataMember on the grid. So if you set it to some completely different data source, the layout you are loading probably won't match up and you might get some odd results.
You can probably use Form_Load, but you just have to make sure that you load the layout after you have set the grid's DataSource.
This worked great. Thanks!
I was trying to load the configuration from the form_load event, but it wasn't working. After having a brain freeze I finally realized I needed run the LoadFromXml in the Ultrawingrid1_InitializeLayout. Is that correct or is there a better place the LoadFromXml should be called from when an application is initially loaded?
thanks for the reply... i surely prefer to write a reliable code than take the risk with the old one
thx
The Encoding usually works okay, but sometimes it will corrupt the data. I'm not sure exactly why, but I have seen it happen.The fact that is works most of the time makes it even more dangerous, since you won't know there is a problem until one of your users suddenly runs into a crash.
Anyway, the StreamReader/StreamWriter approach is more reliable.
string xmlData; private void btnSave_Click(object sender, EventArgs e) { // Create a MemoryStream MemoryStream memoryStream = new MemoryStream(); // Save the grid layout to the MemoryStream. this.ultraGrid1.DisplayLayout.SaveAsXml(memoryStream); // Move back to the beginning of the stream memoryStream.Position = 0; // Create a StreamReader StreamReader streamReader = new StreamReader(memoryStream); // Get the xml text of the stream. this.xmlData = streamReader.ReadToEnd(); } private void btnLoad_Click(object sender, EventArgs e) { // Create a MemoryStream MemoryStream memoryStream = new MemoryStream(); // Create a StreamWriter StreamWriter streamWriter = new StreamWriter(memoryStream); // Write the xml configuration to the stream streamWriter.Write(this.xmlData); streamWriter.Flush(); // Move back to the beginning of the stream memoryStream.Position = 0; // Load the layout. this.ultraGrid1.DisplayLayout.LoadFromXml(memoryStream); }