2013-02-05 74 views
1

我很難嘗試從用戶輸入大量員工數據並按下Submit按鈕的一種表單傳遞信息,並且信息將顯示爲添加的行到一個datagridview表。我能做些什麼來解決這個問題?C#向另一個表格的datagridview添加一行

我目前的代碼:

Form1: 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

     private void getEmployeedata(Manager manager) 
     { 
      int age; 
      int years; 
      int salary;     

      manager.FirstName = firstnameBox.Text; 
      manager.LastName = lastnameBox.Text; 
      manager.Gender = genderBox.Text; 
      manager.Title = titleBox.Text; 
      manager.Exempt = exemptBox.Text; 

      if (int.TryParse(ageBox.Text, out age)) 
      { 
       manager.Age = age; 

       if (int.TryParse(yearsBox.Text, out years)) 
       { 
        manager.Years = years; 

        if (int.TryParse(salaryBox.Text, out salary)) 
        { 
         manager.Salary = salary; 
        } 
        else 
        { 
         MessageBox.Show("Wrong salary input"); 
        } 
       } 
       else 
       { 
        MessageBox.Show("Wrong Years input"); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Wrong age input"); 
      } 
     } 

    private void submitButton_Click(object sender, EventArgs e) 
    { 
     Manager manager = new Manager(); 
     getEmployeedata(manager); 

     EmployeeListing form2 = new EmployeeListing(manager.FirstName, manager.LastName, manager.Gender, manager.Age, manager.Years, manager.Title, manager.Exempt, manager.Salary); 
     form2.Show(); 

    } 

    private void clearButton_Click(object sender, EventArgs e) 
    { 
     firstnameBox.Text = ""; 
     lastnameBox.Text = ""; 
     genderBox.Text = ""; 
     ageBox.Text = ""; 
     yearsBox.Text = ""; 
     titleBox.Text = ""; 
     exemptBox.Text = ""; 
     salaryBox.Text = ""; 
    } 

} 

class Employee 
{ 
    private string firstName = ""; 
    private string lastName = ""; 
    private string gender = ""; 
    private int age = 0; 
    private int years = 0; 

    public string FirstName 
    { 
     get { return firstName; } 
     set { firstName = value; } 

    } 

    public string LastName 
    { 
     get { return lastName; } 
     set { lastName = value; } 
    } 

    public string Gender 
    { 
     get { return gender; } 
     set { gender = value; } 
    } 

    public int Age 
    { 
     get { return age; } 
     set { age = value; } 
    } 

    public int Years 
    { 
     get { return years; } 
     set { years = value; } 
    } 

} //end Employee class 

class Manager : Employee 
{ 
    private string title = ""; 
    private string exempt = ""; 
    private int salary = 0; 

    public string Title 
    { 
     get { return title; } 
     set { title = value; } 
    } 

    public string Exempt 
    { 
     get { return exempt; } 
     set { exempt = value; } 
    } 

    public int Salary 
    { 
     get { return salary; } 
     set { salary = value; } 
    } 

} //end Manager class 

Form2: 

public partial class EmployeeListing : Form 
{ 
    public EmployeeListing(string firstname, string lastname, string gender, int age, int years, string title, string exempt, int salary) 
    { 
      InitializeComponent(); 
      employeeList.Rows.Add(firstname, lastname, gender, age, years, title, exempt, salary);      
    } 

} 
+2

什麼困難?什麼問題?你可以更具體的... –

+0

你可以顯示更多的代碼,例如'employeeList'是如何定義的,並且在哪裏..也顯示了Manager的類。你可能不得不使用關鍵詞'this.firstname,etc ......'來確保正確的變量被分配,但是如果沒有看到更多相關的代碼就不能分辨出來。 – MethodMan

+0

我的歉意。我試圖將用戶輸入的行作爲第二個表單上的datagridview。每當我按下提交按鈕時,它會彈出一個新窗口,顯示一個datagridview,顯示我目前在一行上輸入的內容。它不是創建一個累積列表。 – Hellebus

回答

0

您可以在EmployeeListing()構造函數創建一個DataTable,並設置數據源:

public EmployeeListing(string firstname, string lastname, string gender, int age, int years, string title, string exempt, int salary) 
    { 
      InitializeComponent(); 
      //employeeList.Rows.Add(firstname, lastname, gender, age, years, title, exempt, salary);      

      DataTable dtSource = new DataTable(); 
      dtSource.Columns.Add("firstname", typeof(string)); 
      dtSource.Columns.Add("lastname", typeof(string)); 
      dtSource.Columns.Add("gender", typeof(string)); 
      dtSource.Columns.Add("age", typeof(string)); 
      dtSource.Columns.Add("years", typeof(string)); 
      dtSource.Columns.Add("title", typeof(string)); 
      dtSource.Columns.Add("exempt", typeof(string)); 
      dtSource.Columns.Add("salary", typeof(string)); 
      DataRow dtRow; 

      dtRow = dtSource.NewRow(); 
      dtRow[0] = firstname; 
      dtRow[1] = lastname; 
      dtRow[2] = gender; 
      dtRow[3] = age; 
      dtRow[4] = years; 
      dtRow[5] = Title; 
      dtRow[6] = exempt; 
      dtRow[7] = salary; 

      dtSource.Rows.Add(dtRow.ItemArray); 

      employeeList.DataSource = dtSource; 


    } 

您還應該出這個在其他功能如果您不止一次向GridView添加行,則不在構造函數中。

+0

您需要在Form1中保留'Form2'或'EmployeeListing'的成員變量。否則,每次你實例化'EmployeeListing'來添加一個員工,它將在一個新的實例... –

2

在您的點擊處理程序中,您每次單擊按鈕時都會初始化一個EmployeeListing表單,並且您可能只想這樣做一次。因此,將EmployeeListing的實例保存在點擊處理程序之外,並只創建一個實例以便您可以在隨後的點擊中訪問它。爲了不斷向表單添加數據,您可以在EmployeeListing表單上創建一個公用方法,該方法添加行數據,然後使用表單實例從您的點擊處理程序調用此方法。

這是未經測試,但只是爲了讓你開始...

public partial class Form1 : Form 
     { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void getEmployeedata(Manager manager) 
     { 
      int age; 
      int years; 
      int salary; 
      EmployeeListing form2;  

      manager.FirstName = firstnameBox.Text; 
      manager.LastName = lastnameBox.Text; 
      manager.Gender = genderBox.Text; 
      manager.Title = titleBox.Text; 
      manager.Exempt = exemptBox.Text; 

      if (int.TryParse(ageBox.Text, out age)) 
      { 
       manager.Age = age; 

       if (int.TryParse(yearsBox.Text, out years)) 
       { 
        manager.Years = years; 

        if (int.TryParse(salaryBox.Text, out salary)) 
        { 
         manager.Salary = salary; 
        } 
        else 
        { 
         MessageBox.Show("Wrong salary input"); 
        } 
       } 
       else 
       { 
        MessageBox.Show("Wrong Years input"); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Wrong age input"); 
      } 
     } 

    private void submitButton_Click(object sender, EventArgs e) 
    { 
     Manager manager = new Manager(); 
     getEmployeedata(manager); 
     if (form2 == null) 
     { 
      EmployeeListing form2 = new EmployeeListing(); 
      form2.Show(); 
     }  

     form2.AddRowData(manager.FirstName, manager.LastName, manager.Gender, manager.Age, manager.Years, manager.Title, manager.Exempt, manager.Salary); 
    } 

    private void clearButton_Click(object sender, EventArgs e) 
    { 
     firstnameBox.Text = ""; 
     lastnameBox.Text = ""; 
     genderBox.Text = ""; 
     ageBox.Text = ""; 
     yearsBox.Text = ""; 
     titleBox.Text = ""; 
     exemptBox.Text = ""; 
     salaryBox.Text = ""; 
    } 

} 

class Employee 
{ 
    private string firstName = ""; 
    private string lastName = ""; 
    private string gender = ""; 
    private int age = 0; 
    private int years = 0; 

    public string FirstName 
    { 
     get { return firstName; } 
     set { firstName = value; } 

    } 

    public string LastName 
    { 
     get { return lastName; } 
     set { lastName = value; } 
    } 

    public string Gender 
    { 
     get { return gender; } 
     set { gender = value; } 
    } 

    public int Age 
    { 
     get { return age; } 
     set { age = value; } 
    } 

    public int Years 
    { 
     get { return years; } 
     set { years = value; } 
    } 

} //end Employee class 

class Manager : Employee 
{ 
    private string title = ""; 
    private string exempt = ""; 
    private int salary = 0; 

    public string Title 
    { 
     get { return title; } 
     set { title = value; } 
    } 

    public string Exempt 
    { 
     get { return exempt; } 
     set { exempt = value; } 
    } 

    public int Salary 
    { 
     get { return salary; } 
     set { salary = value; } 
    } 

} //end Manager class 

public partial class EmployeeListing : Form 
{ 
    public EmployeeListing() 
    { 
     InitializeComponent();         
    } 

    public AddRowData(string firstname, string lastname, string gender, int age, int years, string title, string exempt, int salary) 
    { 
     employeeList.Rows.Add(firstname, lastname, gender, age, years, title, exempt, salary); 
    } 
} 
+0

謝謝你的地理。這正是我需要解決的問題。我刪除了表單的實例並創建了調用每行添加的方法。 – Hellebus

相關問題