C#以另一种形式向datagridview添加一行

时间:2013-02-05 01:45:31

标签: c# datagridview rows

我很难尝试从一个表单传递信息,其中用户输入大量员工数据并按下提交按钮,信息将显示为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 :(得分: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); 
    }
 }

答案 1 :(得分: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添加行,你还应该在另一个函数中而不是在构造函数中使用它。