Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Okay, so I'm trying to save ListView with its data through serialization. Using these two methods:
 
public static T BinaryFileDeserialize<T>(string filePath)
        {
            FileStream fileStream = null;
            Object obj;
            try
            {
                if (!File.Exists(filePath))
                    throw new FileNotFoundException("The file" + "was not found.", filePath);
                fileStream = new FileStream(filePath, FileMode.Open);
                BinaryFormatter b = new BinaryFormatter();
                obj = b.Deserialize(fileStream);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
 
            return (T)obj;
        }
and
public static void BinaryFileSerialize(Object obj, string filePath)
        {
            FileStream fileStream = null;
 
            try
            {
                fileStream = new FileStream(filePath, FileMode.Create);
 
                BinaryFormatter b =
                    new BinaryFormatter();
                b.Serialize(fileStream, obj);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
        }
 
Also, since I didn't include or write the methods to fit only this particular purpose of saving ListViews I'm trying - ye, that's correct, trying - to use these two in addition, like helpers:
public ArrayList SaveListView(ListView LV)
        {
            ArrayList alSavedLV = new ArrayList();
 
            for (int i = 0; (i <= animalList.Count - 1); i++)
            {
                alSavedLV.Add(LV.Items[i]);
            }
 
            return alSavedLV;
        }
 
        public ListView LoadListView(ArrayList AL)
        {
            ListView lv = new ListView();
 
            for (int i = 0; (i <= (AL.Count - 1)); i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi = ((ListViewItem)(AL[i]));
                lv.Items.Add(lvi);
            }
 
            return lv;
        }

However, as far as saving goes, I think it works. The deserialization part on the other hand doesn't come through, or at least it doesn't fill my ListView with the saved data. Any ideas? Where might I be going wrong?
 
Happy easter!
Posted 30-Mar-13 2:25am
Comments
toATwork at 31-Mar-13 12:46pm
   
What is the exact error?
leprechauny at 1-Apr-13 1:13am
   
There is no actual error. Or, depending on one's viewpoint, there is. I'm unable to save a file with the current ListView data and then load it again, so to speak. The saving part seems to work, the problems lies with the loading of the saved data.
toATwork at 1-Apr-13 12:07pm
   
Have you enabled exceptions? Did you step by step debug the saving?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Don't serialize/deserialize controls. Have a data layer, serialize the data and use it to populate the controls, and update.
 
—SA
  Permalink  
Comments
Zoltán Zörgő at 1-Apr-13 16:08pm
   
Of course. +5!
Sergey Alexandrovich Kryukov at 1-Apr-13 16:10pm
   
Thank you, Zoltán.
—SA
leprechauny at 3-Apr-13 16:41pm
   
Ye, Sergey, that was pretty much what I was going for. And also what those helper methods was for, but obviously that was where I went wrong, it seems. The idea was that the data would be stored in the ArrayList, in order to avoid serializing the whole ListView control.
 
Anyways, could you specify or give me a hint of a data layer example? So I get an idea of where I'm to be headed.
Sergey Alexandrovich Kryukov at 3-Apr-13 16:47pm
   
ArrayList is the obsolete class; it was made obsolete by the time of the .NET v.2.0. You should use System.Collections.Generic.List<> instead.
—SA
leprechauny at 3-Apr-13 17:41pm
   
Alright. So I'll make some changes to the helper methods so that they return a List<ListViewItem> - that also seems more plausible. Wouldn't one be able to populate the ListView quite easily with such a list as well?
 
Thanks for the quick and great comments by the way.
Sergey Alexandrovich Kryukov at 3-Apr-13 17:56pm
   
You are welcome.
Populate? Yes, sure. Any problem?
—SA
leprechauny at 4-Apr-13 16:04pm
   
Just a simple one. How would I, in the most appropriate way (is there ever one), populate a ListView with a List<ListViewItem>?
 
Thanks in advance!
Sergey Alexandrovich Kryukov at 4-Apr-13 17:34pm
   
Not a problem, but what is the exact type of ListView? (System.Windows.Forms.ListView? System.Windows.Controls.ListView? etc...)
—SA
leprechauny at 5-Apr-13 12:07pm
   
Ah, it's a System.Windows.Forms.ListView. I was thinking that the .AddRange would solve it easily but there is something that I'm missing.
Sergey Alexandrovich Kryukov at 5-Apr-13 13:19pm
   
Please add a tag "WinForms" and always tag the UI library you want to use, otherwise you cannot bring your question to the attention of right people.
 
Now, the major difference: is that detail view or not? With detail vies, it's rather list of lists. If you already have a list of ListViewItem, it does not matter, but you need to populate each item...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100