Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How Can I Pass Multiple Selected Value In Another Childform. This Code Passes Only Single Value In Child Form

//form1//
C#
private void Form1_Load(object sender, EventArgs e)
      {
          SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=C:\Users\Amit\Documents\ghf.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
          SqlDataAdapter sda = new SqlDataAdapter("SELECT pid, pname, cid, pcost FROM product",con);
          DataTable dt = new DataTable();
          sda.Fill(dt);
          dataGridView1.DataSource = dt;
      }

        private void button1_Click(object sender, EventArgs e)
      {
          Form2 f2 = new Form2(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(),
              dataGridView1.SelectedRows[0].Cells[1].Value.ToString(),
              dataGridView1.SelectedRows[0].Cells[2].Value.ToString(),
              dataGridView1.SelectedRows[0].Cells[3].Value.ToString());
          f2.Show();
      }




..//form2//

C#
public Form2(string pid,string pname,string cid,string pcost)
       {
           InitializeComponent();
           dataGridView1.Rows.Add();
           dataGridView1.Rows[0].Cells[0].Value = pid;
           dataGridView1.Rows[0].Cells[1].Value = pname;
           dataGridView1.Rows[0].Cells[2].Value = cid;
           dataGridView1.Rows[0].Cells[3].Value = pcost;
       }
Posted
Updated 26-Jul-15 22:54pm
v2

1 solution

I take it that you mean that you want to pass multiple pids, pnames and so on


One way to do it is to define a class holding the individual values, something like
C#
public class myparam {
   public string pid { get; set; }
   public string pname { get; set; }
   public string cid { get; set; }
   public string pcost { get; set; }
}

And then pass the objects in a list. So the definition for the Form2 constructor could be
C#
public Form2(System.Collections.Generic.List<myparam> listofparams) { ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900