7
Answers

C# structure and listview

Jonathan Crispe

Jonathan Crispe

12y
3.4k
1

I supposed to create a program that will load salary data, consisting of name and salary, from a text file. The data will be stored in a List<> of structures, and displayed using a ListView. The contents of the List<> of structures will be sorted by either the name, or the salary.
 
My Load button is not working: Everytime i click the load button it gives me error message
"Empty path name is not legal"...


       struct SPatient
        {
            public string sName;
            public int iSalary;

            public SPatient(string n, int a)
            {
                sName = n;
                iSalary = a;
            }

            public override string ToString()
            {
                return string.Format("{0}, {1}", sName, iSalary);
            }
        }

        List<SPatient> m_List = new List<SPatient>();
        void SelectionSort()
        {
            int iMinimum = 0;    //index where min value found
            SPatient iTemp;    //temporary storage for swapping
            int iCurrent = 0;    //current location for selected value
            int iScan = 0;    //index to scan the unsorted array

            for (iCurrent = 0; iCurrent < m_List.Count - 1; iCurrent++)
            {
                iMinimum = iCurrent;

                for (iScan = iCurrent + 1; iScan < m_List.Count; iScan++)
                {
                    if (m_List[iScan].iSalary < m_List[iMinimum].iSalary)
                    {
                        iMinimum = iScan;
                    }
                }
                iTemp = m_List[iMinimum];
                m_List[iMinimum] = m_List[iCurrent];
                m_List[iCurrent] = iTemp;
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_SORT_Click(object sender, EventArgs e)
        {
            if (rb_NAME.Checked)
            {
                SelectionSort();
                listView1.Items.Clear();
                foreach (SPatient p in m_List)
                    listView1.Items.Add(p.ToString());
            }
            if (rb_SALARY.Checked)
            {
                listView1.Items.Clear();
                foreach (SPatient p in m_List)
                    listView1.Items.Add(p.ToString());
            }
        }

        private void btn_LOAD_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                BinaryFormatter bf = new BinaryFormatter();
                m_List = (List<SPatient>)bf.Deserialize(fs);
                fs.Close();
                listView1.Items.Clear();
                foreach (SPatient p in m_List)
                    listView1.Items.Add(p.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Answers (7)
Next Recommended Forum