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?