'How to keep the values of datagridview from a child form

enter image description hereI have 3 forms, the main form (f1) has 2 buttons (Pain Reliever and Vitamins C) respectively. If you click the button Pain Reliever,it opens the form 2 alongside with the form 3 (child form). Form 2 (Pain Reliever) has another button for items like for exampleDiclofenac,Gebedol, etc.In the same manner, if you click the button Vitamins C,it opens Form 2 (Vitamins C) alongside with the form 3 (child form) and it has its own button items like Deplin,Vasculera,etc. Form 2 has a button Back which sends back to main form (f1).Form 3 has a datagridview values from either Pain Reliever or Vitamins C but not both. How can i keep the values of pain reliever if i click the button Back(f2) and adds items for Vitamins C ? if i click Back (f2) datagridview will be emptied. I want form 3 to accepts items from both Pain Reliever and Vitamins C.

I really appreciate any help. Thanks

// Here is the code from Form 1 --Load Button is responsible for Loading the buttons (Pain Reliever and Vitamins C) from my Database .

private void FrmMain_Load(object sender, EventArgs e)
{
    LoadButton();
} 


private void LoadButton()
{
        string str = "select [description],count(*) itemCount from dbo.ItmGrp_TBL group by [description]";

        using (SqlConnection stconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["Pharmacy"].ConnectionString))
        {
            using (SqlCommand stc = new SqlCommand(str, stconnect))
            {
                stconnect.Open();

                SqlDataReader reader = stc.ExecuteReader();

                while (reader.Read())
                {
                    Button[] btn = new Button[ (int) reader["itemCount"] ];

                    for(int i=0; i < (int) reader["itemCount"];i++)
                    {
                        btn[i] = new Button() ;
                    }

                    int n = 0;

                    while (n < (int)reader["itemCount"])
                    {
                        btn[n].Width = 180;
                        btn[n].Height = 80;

                        btn[n].Dock = DockStyle.Fill;
                        btn[n].Text = reader["description"].ToString();

                        tlp.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
                        tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 50f));

                        tlp.Controls.Add(btn[n]);

                        btn[n].Click += new EventHandler(ClickButton);

                        n++;
                    }
                }
            }
        }
            
    }

    private void ClickButton(object sender,EventArgs e)
    {
        Button btn = (Button)sender;
        string str = btn.Text;

        frmMenuOrder fm = new frmMenuOrder();

        fm.PassValue = str;
        fm.Show();
        this.Hide();
    }

-- Here is the code for my Form 2 and the child form (Form 3)

    public string PassValue //getting the string values from frmMain
    {
        get { return GetStr; }
        set { GetStr = value; }

    }

   public frmOrder frm = new frmOrder() ;
    private void FrmMenuOrder_Load(object sender, EventArgs e)
    {

        frm.MdiParent = this;  *//Code to load the child form (form 3)*
        frm.Show();
                 
        btnBack.Dock = DockStyle.Bottom;
        LoadMenu();

    }

    private void LoadMenu()
    {
        string str = "select distinct(genericName),ItmGrp,onhand, code,price, count(*) RecCount from ItmPrice_TBL where ItmGrp = '" + GetStr +"'" +" group by GenericName,itmGrp,onhand,code,price  ";

        using (SqlConnection stconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["Pharmacy"].ConnectionString))
        {
            using (SqlCommand stc = new SqlCommand(str, stconnect))
            {
               
                try
                {
                    stconnect.Open();

                    SqlDataReader reader = stc.ExecuteReader();
                    while (reader.Read())
                    {
                        Button[] btn = new Button[ (int)reader["RecCount"] ];

                        for(int i=0; i< (int)reader["RecCount"]; i++)
                        {
                            btn[i] = new Button();
                        }

                        int n = 0;

                        while (n < (int)reader["RecCount"])
                        {
                            btn[n].Left = 80;
                            btn[n].Height = 40;

                            btn[n].Dock = DockStyle.Fill;
                            btn[n].Font = new Font("Microsoft Sans Serif", 14F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                            btn[n].Text = reader["genericName"].ToString();
                                                   
                            tlpMenu.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                            tlpMenu.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));

                            tlpMenu.Controls.Add(btn[n]);

                            ttStr = Convert.ToInt32(reader["onhand"]); //for tool tip string 

                            btn[n].MouseHover += new EventHandler(MouseOver) ;
                           
                            btn[n].Click += new EventHandler(ClickButton);                             

                            n++;
                        }
                    }

                }
                catch(Exception ex)
                {
                    MessageBox.Show("Unable to Load Menu." + ex.Message, "Load Menu Failed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }

    
  
    private void ClickButton(object sender , EventArgs e)
    {
        Button btn = (Button)sender;
        string str = btn.Text;


        string strSql = "select distinct(genericName),price,code from ItmPrice_TBL where genericName = '" + str + "'";


        using (SqlConnection stconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["Pharmacy"].ConnectionString))
        {
            using (SqlCommand stc = new SqlCommand(strSql, stconnect))
            {

                try
                {
                    stconnect.Open();

                    SqlDataReader read = stc.ExecuteReader();

                    while (read.Read())
                    {
                        int row = frm.dgvw.Rows.Add();

                       frm.dgvw.Rows[row].Cells[0].Value = read["code"].ToString();
                        frm.dgvw.Rows[row].Cells[1].Value = read["genericName"].ToString();
                       frm.dgvw.Rows[row].Cells[2].Value = read["price"].ToString();
                    }
                }

                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + " " + ex.StackTrace, "error");
                }
            }
        }

        
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source