Note that I spent too much time trying to get the formatting of this post correct. The formatting is not perfect but I will not fix that.
As far as I know, you still have not specified how the data is saved. That however can affect how the data is processed by the program. I give you credit for asking specific questions. Many questions are quite general; asking how to write an entire program. My reply to many of them is that they need to ask specific questions. You are doing that so I need to give you credit for that. If I knew however how you are saving the data then that would help. The following is intended to show how to access data in anohter form. This is intended to be an object-oriented solution. This is not inended to be a complete program; it is primarily a sample of passing data to another form and getting data from the form.
In this sample, a CSV file of customers is read from and written to. That is done in the "Program" class which is in the Program.cs file. The Program class is static; it must be since it has the Main method which must be static. The class and the method must be static so that Windows can execute the program. So the Program clss in this sample has the following member:
public static BindingList <Customer> Customers = new BindingList <Customer>();
Which is a list of Customer objects. I use a BindingList but a List would probably be the same for your purposes. The Customer class is:
public class Customer
{
public string id { get ; set ; }
public string name { get ; set ; }
}
Of course your Customer class has more members but this is just a sample. The data is read (using GetCustomers()) before the main form is shown and then when the main form is closed the data is written (using PutCustomers()). So the Main has the following:
Application .EnableVisualStyles();
Application .SetCompatibleTextRenderingDefault(false );
GetCustomers();
Application .Run(new Form1 ());
PutCustomers();
And the following are the GetCustomers() and PutCustomers():
private static void GetCustomers()
{
char [] delimiters = { , };
String [] itemarray;
Customer c;
FileInfo fi = new FileInfo (Filename);
if (!fi.Exists)
return ;
using (StreamReader file = new StreamReader (Filename))
{
while (!file.EndOfStream)
{
itemarray = file.ReadLine().Split(delimiters, 2);
if (itemarray.Length == 2)
{
c = new Customer ();
Customers.Add(c);
c.id = itemarray[0];
c.name = itemarray[1];
}
}
}
}
private static void PutCustomers()
{
using (StreamWriter file = new StreamWriter (Filename))
{
foreach (Customer c in Customers)
{
file.WriteLine(c.id + +c.name);
}
}
}
However note that the code shown above is incomplete; the tool I use to format source code is not perfect. So the following are two lines that are shown without the formatting tool that are the correct version of the code above.
char[] delimiters = { '\t', ' ' };
file.WriteLine(c.id +'\t'+c.name);
Okay so that is how the program reads and writes the data. The main form of the program has a DataGridView and a menu bar. The form has a BindingSource as a member; it is:
BindingSource bs = new BindingSource ();
The Customers list is bound to the DataGridView in the form load event using:
bs.DataSource = Program .Customers;
dataGridView1.DataSource = bs;
That is all that is needed to bind the data to the control. You can download the attached project to see how it works.
Note that a DataGridView allows rows to be added and edited in it, so we do not need a second form. If however you do not want to use a DataGridView then this sample is useful for showing how to use a second form to edit the data; you do not have to use a DataGridView in your program. The main form also has a menu bar with commands to add or edit customers using a second form. The following code will use the second form to add a record:
Customer c = new Customer ();
Form2 f = new Form2 (c);
DialogResult dr = f.ShowDialog();
if (dr != DialogResult .OK)
return ;
Program .Customers.Add(c);
Program .Customers.ResetBindings();
In this sample, a row in the DataGridView needs to be selected for the edit to happen. The following will edit the selected row:
if (dataGridView1.SelectedRows.Count != 1)
{
MessageBox .Show("Nothing selected" );
return ;
}
Customer c = dataGridView1.SelectedRows[0].DataBoundItem as Customer ;
Form2 f = new Form2 (c);
DialogResult dr = f.ShowDialog();
if (dr != DialogResult .OK)
return ;
Program .Customers.ResetBindings();
The important thing here is that the Form2 object is being created with a constructor that takes a Customer object as a parameter. That is the important thing and it is very fundamental to object-oriented programming and languages such as C#, Java and C++. There is nothing exotic or advanced about doing that. The Form2 class has a Customer object as a member; as in:
Customer c = null;
The Form2 class has a constructor that takes a Customer object as a parameter; the following is the complete constructor:
public Form2(Customer c)
{
InitializeComponent();
this .c = c;
textBoxId.Text = c.id;
textBoxName.Text = c.name;
}
The Constructor also sets the textboxes to the values of the Customer object. Then the Okay button click event does the following:
DialogResult = DialogResult .OK;
c.id = textBoxId.Text;
c.name = textBoxName.Text;
Close();
The Cancel button click event does the following:
DialogResult = DialogResult .OK;
c.id = textBoxId.Text;
c.name = textBoxName.Text;
Close();
The relevant portion of code is very simple. We simply pass an object to the constructor and the form uses just one object. The object (Customer) can have many more properties and regardless of how many properties it has, the constructor only needs one parameter.