C # - Passing data between Windows Form using ListView

0

I have a project in C # Windows Form, in my project I have 2 screens, 1 - ListView to show the information, 2 Form to get the user data and pass to the ListView of Form1, but the problem is: pass the form2 information for form1

My Logic:

  • Get user information through form2 and pass it to another class
  • In Form1 I read the information from this other class and step into the ListView

Problem:

  • The object of type ListViewItem always returns nullo

Form2: For information

    namespace Company
{
    public partial class Register : Form
    {
        EmployeeDAO employeeDAO = new EmployeeDAO();

        public Register()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            Employee employee = new Employee();
            employee.idEmployee = Convert.ToInt16(this.txtId.Text);
            employee.nameEmployee = this.txtName.Text;
            employeeDAO.insert(employee);

        }

    }
}

My class to get information from form2 and switch to form1: (Placing in ListViewItem)

namespace Company
{
    class EmployeeDAO
    {
        ListViewItem item = new ListViewItem();

        public void insert(Employee employee)
        {
            string id;
            string name;
            id = Convert.ToString(employee.idEmployee);
            name = employee.nameEmployee;
            String[] row = { id, name };
            item = new ListViewItem(row);
        }

        public ListViewItem read()
        {
            //This item are returning null
            return item;
        }
    }
}

namespace Company
{
    public partial class Main : Form
    {

        public Main()
        {
            InitializeComponent();
            lstEmployee.View = View.Details;
            lstEmployee.FullRowSelect = true;
            lstEmployee.Columns.Add("ID", 150);
            lstEmployee.Columns.Add("Nome", 150);
            insert();
        }

        private void insert()
        {
            EmployeeDAO employeeDAO = new EmployeeDAO();
            ListViewItem item = employeeDAO.read();
            if (item == null)
            {
                //Always this block run
                MessageBox.Show("No Item");
                return;
            }
            else
            {
                MessageBox.Show("Item");
                lstEmployee.Items.Add(item);
            }            
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            Register register = new Register();
            register.Show();
            this.Hide();
        }
    }
}

Can anyone help me with this?

    
asked by anonymous 29.06.2017 / 21:16

1 answer

0

You can add a non-required parameter in the constructor of your Main class:

public partial class Main : Form
{
    public Main(Dictionary<int, string> listEmployee = new Dictionary<int, string>)
    {
                    InitializeComponent();
                    lstEmployee.View = View.Details;
                    lstEmployee.FullRowSelect = true;
                    lstEmployee.Columns.Add("ID", 150);
                    lstEmployee.Columns.Add("Nome", 150);
                    insert();
    }

    private void insert()
    {          
                    foreach(var item in listEmployee)
                    lstEmployee.Items.Add(new ListViewItem(string[] {item.Key.toString(), item.Value}));    
    }

    private void btnRegister_Click(object sender, EventArgs e)
    {
                    Register register = new Register();
                    register.Show();
                    this.Hide();
    }
}

And your Register:

public partial class Register : Form
{
        Dictionary<int, string> listEmployee = new Dictionary<int, string>

        public Register()
        {
                InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
                listEmployee.Add(Convert.toInt32(this.txtId.Text), this.txtName.Text);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
                var main = new Main(listEmployee);
        }

 }

I do not think it makes sense to have those intermediate classes (EmployeeDAO). In addition, in the main you instantiate it again, which means that it is a new object, without any attribution that is not made directly in its constructor or directly in its attributes.

    
29.06.2017 / 23:03