排序组合框时出现问题

时间:2016-04-13 15:21:14

标签: c# asp.net combobox

我正在处理一项任务,我需要通过List填充comboBox,并从employee.dat文件填充List。我成功地填充了组合,但在排序时我面临着一个问题。我尝试排序的值是字符串数字。它显示了0-9的值,但是当它超过9时,组合看起来很傻。

这是截图

enter image description here

我需要做的是对这些值进行排序,平均值10应该以9为前提。

以下是我尝试过的代码段。

         private void FormDelete_Load(object sender, EventArgs e)
    {
        //LOAD EVENT for FORM

        //Clear all form controls
        Reset();

        //Fill the ID Combo box using data in List<T>
        comboBoxID.Items.Clear();
        foreach (MyClass.Employee obj in MyClass.listEmployees)
        {
            comboBoxID.Items.Add(obj.ID);
        }

        //Sort the combo box ibn ascending order
        comboBoxID.Sorted = true;

    }

Employee.cs

    public class Employee
    {
        public int ID;
        public string fName;
        public string lName;
        public string gender;
        public int age;
        public double hourWage;

    }
    public static List<Employee> listEmployees = new List<Employee>();

Empolyee.dat

1, Ann, Crowe, F, 34, 12.95
2, Bob, Costas Jr., M, 27, 8.75
3, Sue, Soppala, F, 22, 7.95
4, Bill, Barton, M, 45, 15.25
5, Jill, Jordan, F, 33, 14.75
6, Art, Ayers, M, 33, 14.75
7, Larry, Stooge, M, 55, 21.05
8, Art, Ayers, M, 33, 14.75
9, Larry, Stooge, M, 55, 21.05
10, Art, Ayers, M, 33, 14.75
11, Larry, Stooge, M, 55, 21.05

列出填充代码

 if (File.Exists("employee.data"))
        {
            try
            {
            streamEmployee = new StreamReader("employee.data");  
            string line;  //to read a line from the text file
            string[] arrFields = new string[5];
            while (streamEmployee.Peek() > -1)
            {
                line = streamEmployee.ReadLine();  //read a line of records      from file
                arrFields = line.Split(',');  //split the line at comma junction, and save split                            //fields in array 

                MyClass.Employee objEmp = new MyClass.Employee();  //create a "specific" employee                                       //object instance

                //Assign each field from line as generic object's properties to make it "specific
                objEmp.ID = Convert.ToInt32(arrFields[0].Trim());  //ID is integer
                objEmp.fName = arrFields[1].Trim();
                objEmp.lName = arrFields[2].Trim();
                objEmp.gender = arrFields[3].Trim();
                objEmp.age = Convert.ToInt32(arrFields[4].Trim());  //age is integer
                objEmp.hourWage = Convert.ToDouble(arrFields[5].Trim());  //hourly wage is double

                //Add this specific employee object to the List
                MyClass.listEmployees.Add(objEmp);

            } //while


        } //try

        catch (IOException err)
        {
            MessageBox.Show(err.Message);
            error = true;

        } //catch

        finally
        {
            if (streamEmployee != null)  //it is indeed representing a file and may not be closed
                streamEmployee.Close();


        } //finally

    } //if

1 个答案:

答案 0 :(得分:3)

您的文件已经排序,因此无需调用Sorted = true 无论如何,如果你想确定输入文件中存在的任何顺序,你的代码会按照ID顺序添加员工,那么你可以将你的循环更改为

foreach (MyClass.Employee obj in MyClass.listEmployees.OrderBy(x => x.ID))
{
    comboBoxID.Items.Add(obj.ID);
}

再次无需将Sorted属性设置为true ....