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
Stream to string
System.IO.MemoryStream msLayout = new System.IO.MemoryStream();Byte[] byteLayout = msLayout.ToArray();string strLayout = Encoding.UTF8.GetString(byteLayout);
string to stream
string strLayout;Byte[] byteRibbon = Encoding.UTF8.GetBytes(strLayout);System.IO.MemoryStream msLayout= new System.IO.MemoryStream(byteLayout);
Using an Encoder to convert the stream into a string is a really bad idea. The whole point of an Encoder is to manipulate the stream and coerce it into a string of a particular encoding type. Modifying the stream in this way is basically just asking for it to be damaged.
If you want to convert the stream into a string, a better way to do it would be to use the StreamReader class - and then use a StreamWriter to get it back into a stream.
Hi Mike,
Thanks for the warning. I've been using this method and it wasn't making any troubles.
Anyway if you say that it is a bad idea, i beleive you. but can you please give a little sample on how to use the StreamReader and StreamWriter to convert?
thx
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
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); }