Hellebus
Hellebus

Reputation: 11

C# adding a row to a datagridview in another form

I'm having difficulty trying to pass information from one form in which a user inputs a lot of employee data and presses a Submit button and the information is to display as an added row to a datagridview table. What can I do to fix this issue?

The code I have at present:

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);                      
    }

 }

Upvotes: 1

Views: 1938

Answers (2)

Geo242
Geo242

Reputation: 597

In your click handler, you are initializing a EmployeeListing form every time you click the button, and you probably only want to do this once.So, keep the instance of your EmployeeListing outside of the click handler and only create one instance of it so that you can access it on subsequent clicks. To keep adding data to the form, you could create a public method on the EmployeeListing form that adds the row data and then call this method from your click handler, using the instance of the form.

This is untested, but just to get you started...

    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); 
    }
 }

Upvotes: 2

whastupduck
whastupduck

Reputation: 1166

You can create a DataTable on the EmployeeListing() constructor and set the 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;


    }

You should also out this in another function and not in the constructor if you will add rows to the gridview more than once.

Upvotes: 0

Related Questions