Click here to Skip to main content
15,884,975 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        int pid = Convert.ToInt32(dataGridView3.SelectedRows[0].Cells[0].Value);
        string pname = dataGridView3.SelectedRows[0].Cells[1].Value.ToString();
        double pamount = Convert.ToDouble(dataGridView3.SelectedRows[0].Cells[2].Value.ToString());
       int cid = Convert.ToInt32(dataGridView3.SelectedRows[0].Cells[3].Value);
       string category = dataGridView3.SelectedRows[0].Cells[4].Value.ToString();

            Form2 f2 = new Form2(pid, pname, pamount, cid, category);
            f2.Show();

    }



 public Form2(int pid, string pname, double pamount, int cid, string category)
    {
        InitializeComponent();

          dg1.Rows.Add(
       dg1.Rows[0].Cells[0].Value = pid,
       dg1.Rows[0].Cells[1].Value = pname,
       dg1.Rows[0].Cells[2].Value = pamount,
       dg1.Rows[0].Cells[3].Value = cid,
       dg1.Rows[0].Cells[4].Value = category);
    }
Posted

Sorry, the formatting is messed up, but try this...

private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
    {

 
            Form2 f2 = new Form2(dataGridView3.SelectedRows);
            f2.Show();
 
    }

 public Form2(List<datagridviewrow> rows)
    {
        InitializeComponent();
 

        foreach(DataGridViewRow dgr in rows)
        {
          dg1.Rows.Add(row);
    }
 
Share this answer
 
v3
now you have constructor like below
C#
public Form2(int pid, string pname, double pamount, int cid, string category)
    {

add another constructor to send list of items as you wish. for example
C#
public Form2(DataGridViewSelectedRowCollection rows)
{
   InitializeComponent();
  // do something with rows 
}

can be used to send all selected rows
C#
Form2 f2 = new Form2(dataGridView3.SelectedRows);
 
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