我很難嘗試從用戶輸入大量員工數據並按下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);
}
}
什麼困難?什麼問題?你可以更具體的... –
你可以顯示更多的代碼,例如'employeeList'是如何定義的,並且在哪裏..也顯示了Manager的類。你可能不得不使用關鍵詞'this.firstname,etc ......'來確保正確的變量被分配,但是如果沒有看到更多相關的代碼就不能分辨出來。 – MethodMan
我的歉意。我試圖將用戶輸入的行作爲第二個表單上的datagridview。每當我按下提交按鈕時,它會彈出一個新窗口,顯示一個datagridview,顯示我目前在一行上輸入的內容。它不是創建一個累積列表。 – Hellebus