Ganewt
Ganewt

Reputation: 19

c# OOP Winforms -Pls Help /transfer listdata from form2 to form1 datagridview/without Database

I know this is a frequent question and I have searched for too long for a answerthat helps me.any help would be much appreciated I have 2 window forms --form1 has add,edit delete refresh buttons with datagridview

form2 pops up when add button in form1 clicked.this works o.k form2 has few input text boxes name,age,city etc. with save button

******Not using Datatabase***** My question is how do you display the input data on form2 to datagrid view on form1.The column headings data is there but not inputted list data here is my code so far:

FORM1

    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }
    StuAdd sa = new StuAdd();
    ListClass lc = new  ListClass();
    BindingSource bs = new BindingSource();

    private void btnAddS_Click(object sender, EventArgs e)
    {
            sa.Show();

    }

    private void btnRefreshS_Click(object sender, EventArgs e)
    {
        bs.DataSource = lc.myList;
        dgv1.DataSource = bs;

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }





}

FORM2

     public partial class StuAdd : Form
    {



        public StuAdd()
        {
            InitializeComponent();


        }

        ListClass lc = new  ListClass();

        private void btnSave_Click(object sender, EventArgs e)
        {
            string title = cboTitle.SelectedItem.ToString();
            string fname = txtFname.Text;
            string lname = txtLname.Text;
            string dob = dtpDOB.Text;
            int stuId = int.Parse(txtSID.Text);
            string status = cboStatus.SelectedItem.ToString();
            string phone = txtPhone.Text;
            string email = txtEmail.Text;


            lc.AddStudent(title,fname, lname, dob,stuId,status,phone,email);

           MessageBox.Show("Data Added", "Message",        MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            txtEmail.Clear();
            txtFname.Clear();
            txtLname.Clear();
            txtPhone.Clear();
            txtSID.Clear();
            this.Hide();


        }

        private void StuAdd_Load(object sender, EventArgs e)
        {

        }
    }
}

STUDENT CLASS

    class Student:Person
       {
     public int StudentID { get; set; }
     public string Status { get; set; }
     public string MyString()
     {
        StringBuilder sb = new StringBuilder();
        sb.Append(base.GetAll());
        sb.Append(" ");
        sb.Append(StudentID);
        sb.Append(" ");
        sb.Append(Status);





        return string.Format("{0} {1} {2} {3} {4} {5} {6} {7} ", Title, FirstName, Lastname, DOB, StudentID, Status, Email, Phone);
    }


}

} PERSON CLASS

    class Person
    {
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string Lastname { get; set; }
    public string DOB { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }

    public string GetAll()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(Title);
        sb.Append(" ");
        sb.Append(FirstName);
        sb.Append(" ");
        sb.Append(Lastname);
        sb.Append(" ");
        sb.Append(DOB);
        sb.Append(" ");
        sb.Append(Phone);
        sb.Append(" ");
        sb.Append(Email);
        return sb.ToString();
    }

LISTCLASS class ListClass

   {
    public List<Student> myList = new List<Student>();


    public string AddStudent(string ti, string fn, string ln, string dob,   int sid, string stat, string ph, string em)
    {
        Student s = new Student();
        s.Title = ti;
        s.FirstName = fn;
        s.Lastname = ln;
        s.DOB = dob;
        s.StudentID = sid;
        s.Status = stat;
        s.Phone = ph;
        s.Email = em;

        myList.Add(s);

        return "Student added to List";
    }

}

}

Upvotes: 0

Views: 56

Answers (1)

JohnG
JohnG

Reputation: 9469

There are many articles explaining how to do this. In addition, there is more than one way to do this. However, following S.Petrosov comment, one possible solution is to PASS the variable you want to change to form 2. If you want the second form to make changes to a form 1 control(s), then you either have to make those controls public or PASS the variables that you want changed to the second form.

Using the posted code, there are a couple of thing to change to achieve this. First, you need to identify WHICH control/variable you want Form 2 to change. In this case, this variable appears to be the lc variable which appears to hold a list of Student objects. This will work, HOWEVER, in Form 1 it also appears that the code is using a BindingSource for the DataGridView. Since the BindingSource is used as a DataSource to the DataGridView AND this is the source of the data you want to add new Students to from Form 2, then this variable bs appears to be the control/variable we want to pass to form 2.

Since this BindingSource is used as a DataSource for the DataGridView in Form 1, and we want to pass this variable to Form 2, Setting this DataSource can be done when Form 1 is Loaded. Therefore, I moved the setting of this DataSource from the btnRefreshS_Click event to Form 1’s Load event like below:

private void Form1_Load(object sender, EventArgs e) {
  //AddExistingData?();
  bs.DataSource = lc.myList;
  dgv1.DataSource = bs;
}

Now the BindingSource’s DataSource is set to the ListClass’s myList of Students. Then this BindingSource is set as a DataSource to Form 1’s DataGridView. Initially the DataGridView will be empty unless you add some pre-existing data.

Now in Form 1, we simply need to pass this BindingSource (bs) to StuAdd form when StuAdd form is initialized in btnAddS_Click event like below:

private void btnAddS_Click(object sender, EventArgs e) {
  StuAdd sa = new StuAdd(bs);
  sa.Show();
}

These are the only changes needed to Form 1. Now in StuAdd Form we need to change StuAdd’s constructor at accept the parent forms BindingSource we want to add a student to. The StuAdd form will need to change its signature AND add a BindingSource variable so the other methods can use it. Below are the changes as described above.

BindingSource parentBS;  // <- variable other methods in `StuAdd` use

public StuAdd(BindingSource bs) {
  InitializeComponent();
  parentBS = bs;
}

Finally, in StuAdd’s btnSave_Click event, you will need to create a new Student object from the various controls on the form since the BindingSource is using a list of Student objects. The following line of code will NOT work when adding a new Student to the BindingSource which is really where you want to add the new Student.

lc.AddStudent(title,fname, lname, dob,stuId,status,phone,email);

Below are the changes made to btnSave_Click event to add a new student to the parent forms BindingSource. Please note I simply created a new Student object from the forms controls then added the new Student to parentBS. Since the form appears to hide/close after the button is clicked… I simply closed the form. After this is done, the new Student data should appear at the bottom of the DataGridView. There should be no need to re-bind the DataGridView and the btnRefresh_Click event is really unnecessary.

Again, this is one way to achieve this; there are other possible better ways to achieve this. Another note is there appears to be NO error checking when adding students. Mainly, the line int stuId = int.Parse(txtSID.Text); will crash if the user types in alpha characters or anything that is NOT a number. You may want to look at the int.TryParse method. Hope this helps.

private void btnSave_Click(object sender, EventArgs e) {
  string title = cboTitle.SelectedItem.ToString();
  string fname = txtFname.Text;
  string lname = txtLname.Text;
  string dob = dtpDOB.Text;
  int stuId = int.Parse(txtSID.Text);
  string status = cboStatus.SelectedItem.ToString();
  string phone = txtPhone.Text;
  string email = txtEmail.Text;
  Student newStudent = new Student();
  newStudent.Title = title;
  newStudent.FirstName = fname;
  newStudent.Lastname = lname;
  newStudent.DOB = dob;
  newStudent.StudentID = stuId;
  newStudent.Status = status;
  newStudent.Phone = phone;
  newStudent.Email = email;

  parentBS.Add(newStudent);

  MessageBox.Show("Data Added", "Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

  //txtEmail.Clear();
  //txtFname.Clear();
  //txtLname.Clear();
  //txtPhone.Clear();
  //txtSID.Clear();
  this.Close();
}

Upvotes: 0

Related Questions