Reputation: 949
I'm working on a task in which I need to populate the comboBox through List and the List is populating from employee.dat
file. I'm successfully populates the combo but while sorting it I'm facing an issue. The Values which I'm trying to sort is String Numbers. It shows the values from 0-9 but when it beyond from 9 the combo looks goofy.
Here is the screenshot
What I need to do is sort these values, Means 10 should be leading by 9.
Here is the code snippet which I have tried yet.
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
List Populating code
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
Upvotes: 1
Views: 119
Reputation: 216303
Your file is already sorted, thus there is no need to call Sorted=true.
Anyway, if you want to be sure that, whatever order is present in the input file, your code adds the employees following the ID order, then you could change your loop to
foreach (MyClass.Employee obj in MyClass.listEmployees.OrderBy(x => x.ID))
{
comboBoxID.Items.Add(obj.ID);
}
Again no need to set the Sorted property to true....
Upvotes: 3