2014-02-06 7 views
0

저는 텍스트 파일에서 읽는 직원 목록을 보관할 데이터베이스를 설계하고 있습니다. 두 가지 형식이 있는데 첫 번째 것은 (frmManager) 목록을 통과하는보기 역할을합니다. 목록의 직원을 스크롤하는 다음 및 이전 단추가 있습니다. 다른 양식 (frmAdd)은 새 직원을 목록에 추가 할 수 있습니다. 내 문제는, 내가 목록 < 업데이트 할 때, 어떻게 frmManager에서 업데이트 할 수 있습니까? 새 직원을 추가하면 theiir 특성이 텍스트 파일에 기록되지만 업데이트 된 목록을 표시하도록 프로젝트를 다시 작성해야합니다. 직원을 추가 파일 :양식에 업데이트 된 목록 표시

public class EmployeeDB 
{ 
    public List<Employee> employees; 

    public static EmployeeDB instance; 

    public static EmployeeDB Instance 
    { 
     get 
     { 
      if (instance == null) 
      { 
       instance = new EmployeeDB(); 
       instance.populate(); 
      } 
      return instance; 
     } 
    } 

    public EmployeeDB() 
    { 
     employees = new List<Employee>(); 
    } 

    public void populate() 
    { 
     string[] parts; 
     foreach (string line in File.ReadAllLines("StaffList.txt")) 
     { 
      parts = line.Split(','); 
      employees.Add(new Employee(parts[0], parts[1], parts[2], parts[3], int.Parse(parts[4]), int.Parse(parts[5]))); 

     } 
    } 
} 

직원 클래스는 자신의 세부 정보를 추가하는 단지 생성자가 포함되어 있습니다. 양식 신입 사원

public partial class frmAdd : Form 
{ 

    EmployeeDB employee; 
    int grade; 

    public frmAddEmployee() 
    { 
     employee = EmployeeDB.Instance; 
     InitializeComponent(); 
    } 

    private void btnCreate_Click(object sender, EventArgs e) 
    { 
     System.IO.StreamWriter file = new System.IO.StreamWriter("StaffList.txt", true); 

     foreach (Employee em in employee.employees) //To avoid username clashes 
     { 
      if (em.username == txtUsername.Text) 
      { 
       file.WriteLine(txtFName.Text + "," + txtLName.Text + "," + txtUsername.Text + employee.employees.Count() 
           + "," + txtPassword.Text + "," + checkedButton().ToString() + "," 
           + 0.ToString(), Environment.NewLine); 
      } 
      else 
      { 
       file.WriteLine(txtFName.Text + "," + txtLName.Text + "," + txtUsername.Text 
             + "," + txtPassword.Text + "," + checkedButton().ToString() + "," 
             + 0.ToString(), Environment.NewLine); 
      } 

      file.Close(); 
      MessageBox.Show("Employee successfully added"); 
      return; 
     } 

나는 그것이 아무 소용이 다시 채울 것입니다 바라고 EmployeeDB 파일을 불러 시도를 추가합니다. 어떤 도움을 많이 주시면 감사하겠습니다.

+0

위의 R => r.username 왜 당신이 _actual_ 데이터베이스를 사용하지 않는 클래스 직원이라는 이름의 문자열이 가정입니다? – DGibbs

+0

원래 데이터베이스를 사용해 보았지만 대학 네트워크에서 일하고있어 로컬 인 경우에도 데이터베이스와 연결되는 것을 좋아하지 않는 것 같습니다. – Sawyer05

답변

1

새 직원을 어디에도 추가하지 않습니다. 포함 된 코드에서 populate()를 통해서만 목록을 채 웁니다.

새 직원을 단추 클릭시 EmployeeDB에 추가합니다. 이렇게하면 목록이 최신 상태가됩니다. 그런 다음 파일을 쓰는 EmployeeDB 클래스에 메서드를 추가합니다.

private void btnCreate_Click(object sender, EventArgs e) 
{ 
    string username = txtUsername.Text; 
    int i = employee.employees.Count(); 
    while (employee.AsEnumerable().Select(r => r.username == username).Count() > 0) 
    { 
     username = txtUserName.Text + i++; //Makes sure you have no common usernames 
    } 
    employee.employees.Add(new Employee(){...}); 
    employee.SaveFile(); //New method 
} 

난 당신이 따라서

+0

감사합니다. Garrick, 처음부터 다시 시작하여 프로젝트를 정리했습니다. 주변 상황을 변화시키는 혼란을 일으켰습니다. 나는이 시간 동안 당신의 충고를 탑승 할 것입니다. – Sawyer05