1

我在尝试从一种形式传递信息时遇到困难,在这种形式中,用户输入大量员工数据并按下提交按钮,并且信息将作为添加行显示到 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);                      
    }

 }
4

2 回答 2

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); 
    }
 }
于 2013-02-05T02:07:41.350 回答
0

您可以在 EmployeeListing() 构造函数上创建一个 DataTable 并设置 DataSource:

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 添加行,您还应该在另一个函数中而不是在构造函数中输出它。

于 2013-02-05T02:01:18.777 回答