private void LoadDataFromFile(string filePath)// Đọc dữ liệu từ file và đưa lên datagridview { //try { StreamReader sr = new StreamReader(filePath, Encoding.UTF8);//tạo một streamreader để đọc file theo đường dẫn là biến filePath với kiểu mã UTF8 để đọc tiếng việt có dấu string readInformation; //biến để lưu thông tin đọc đc từng dòng while ((readInformation = sr.ReadLine()) != null) //trong khi còn dòng để đọc (sr.readline: lấy ra một dòng trong file text) { if (readInformation.Contains("Họ và tên\t\tChức vụ\t\t\tNgày sinh\t\tHệ số lương")) { continue; //Nếu thông tin đọc dc có chứa chuỗi trong ngoặc (tức là dòng đầu tiên trong file text) thì bỏ qua các lệnh ở dưới và quay lại lệnh while } string[] splitedString = readInformation.Split('\t'); //Cắt chuỗi đọc dc bằng hàm split và bỏ wa kí tự \t để tách các thông tin của nhân viên ra //ví dụ: readInformation = "nhok kon nguyen\t\tTổng giám đốc\t\t19/06/2017\t\t12" /* thì splitedString[]: * splitedString[0] = "nhok kon nguyen" * splitedString[0] = "" * splitedString[0] = "Tổng giám đốc" * splitedString[0] = "" * splitedString[0] = "19/06/2017" * splitedString[0] = "" * splitedString[0] = "12" */ string[] employeeInformation = new string[splitedString.Length]; int j = 0; //duyệt từng phần tử của mảng splitedString nếu rỗng thì bỏ wa, khác rổng thì thêm vào mảng employeeInformation foreach (string item in splitedString) { if (item == "") { continue; } else { employeeInformation[j] = item; j++; } } Date birthDay; Node <Employee> employee; birthDay = Date.Parse(employeeInformation[2]); //tạo một node để thêm vào listEmployee employee = new Node <Employee>() { Data = new Employee(employeeInformation[0], employeeInformation[1], birthDay, double.Parse(employeeInformation[3])), Next = null }; //Thêm vào danh sách listEmployee.AddLast(employee); //Đọc dòng tiếp theo readInformation = sr.ReadLine(); } //dóng file sr.Close(); } }
private void LoadDataFromFile(string filePath)// Đọc dữ liệu từ file và đưa lên datagridview { //try { StreamReader sr = new StreamReader(filePath, Encoding.UTF8); int countLine = 0; string readInformation; while ((readInformation = sr.ReadLine()) != null) { countLine++; if (readInformation.Contains("Họ và tên\t\tChức vụ\t\t\tNgày sinh\t\tHệ số lương")) { continue; } if (readInformation == "") { continue; } string[] splitedString = readInformation.Split('\t'); string[] employeeInformation = new string[splitedString.Length]; int j = 0; foreach (string item in splitedString) { if (item == "") { continue; } else { employeeInformation[j] = item.Trim(); j++; } } Date birthDay; Node <Employee> employee; birthDay = Date.Parse(employeeInformation[2]); employee = new Node <Employee>() { Data = new Employee(employeeInformation[0], employeeInformation[1], birthDay, double.Parse(employeeInformation[3])), }; listEmployee.AddLast(employee); //try //{ // birthDay = Date.Parse(employeeInformation[2]); //} //catch (DayException) //{ // MessageBox.Show("Tại dòng " + countLine + " trong file: Ngày sinh không hợp lệ\n" + DayException.ErrorMessage, "Lỗi định dạng ngày giờ khi lấy dữ liệu từ file", MessageBoxButtons.OK, MessageBoxIcon.Error); // sr.Close(); // return; //} //try //{ // employee = new Node<Employee>() // { // Data = new Employee(employeeInformation[0], // employeeInformation[1], // birthDay, // double.Parse(employeeInformation[3])), // }; // listEmployee.AddLast(employee); //} //catch (Exception) //{ // MessageBox.Show("Tại dòng " + countLine + " trong file:\n" + "Hệ số lương phải là số", "Lỗi định dạng hệ số lương khi lấy dữ liệu từ file", MessageBoxButtons.OK, MessageBoxIcon.Error); // sr.Close(); // return; //} readInformation = sr.ReadLine(); } sr.Close(); ShowListEmployeeToDatagridView(this.listEmployee); } }