Hashtable 是散列表,就是说元素的存储位置是散列的,而不是有序的,也就是说,不是按我们放进去的顺序就行排列的,楼主可以把Hashtable  换成arraylist,如果确实需要 健-值 集合的话就是用 SortedList

解决方案 »

  1.   

    SortedList 会对健进行排序,可能还是不是你想要的那种结果,所以还是用 ArrayList吧。
      

  2.   

    HashTable是不可以排序的,可以使用Arraylist实现
      

  3.   

    那就用SortedList 吧,把键值前加个英文字母 编号--A编号 姓名--B姓名
    部门--C部门 工资--D工资,
    这样在SortedList 的排序就是 A编号,B姓名,C部门,D工资了。
      

  4.   

    根本就没有必要用HashTableusing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.IO;namespace Diy
    {
        public partial class Form1 : Form
        {
            private StreamWriter sw;
            private const string path = @"E:\ACCP4.0-S2\C#\第十二章\Diy\Employee.txt";
            public static string _empId;
            public static string _empName;
            public static string _empDepart;
            public static double _empSalary;
            public Form1()
            {
                InitializeComponent();
            }        private void btnAdd_Click(object sender, EventArgs e)
            {
                if (File.Exists(path))
                {
                    sw = File.AppendText(path);
                }
                else
                {
                    sw = File.CreateText(path);
                }
                //向Hashtable中添加元素
                Employee obj = new Employee();
                obj.empId = this.txtEmpId.Text;
                obj.empName = this.txtEmpName.Text;
                obj.empDepart = this.txtEmpDepart.Text;
                obj.empSalary = Convert.ToDouble(this.txtEmpSalary.Text);
                //写入文件流中
                try
                {
                    sw.WriteLine("编号:"+obj.empId);
                    sw.WriteLine("姓名:"+obj.empName);
                    sw.WriteLine("部门:"+obj.empDepart);
                    sw.WriteLine("工资:"+obj.empSalary.ToString());
                    MessageBox.Show("职员信息录入成功!");
                    this.txtEmpId.Text = "";
                    this.txtEmpId.Focus();
                    this.txtEmpName.Text = "";
                    this.txtEmpDepart.Text = "";
                    this.txtEmpSalary.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    sw.Close();
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
               
            }        private void btnSearch_Click(object sender, EventArgs e)
            {
                frmSearch fs = new frmSearch();
                fs.ShowDialog();
                this.txtEmpId.Text = _empId;
                this.txtEmpName.Text = _empName;
                this.txtEmpDepart.Text = _empDepart;
                this.txtEmpSalary.Text = _empSalary.ToString();
            }
        }