user2837847
user2837847

Reputation: 137

Pass selected row gridview to another gridview on another form

as i am still new to c#, i am having trouble passing selected row from datagridview1(form 1) to datagridview2 (form 2) Assuming i have to pass a large number of columns, how do i do it?

And if it's possible, can i do it while selecting multiple rows and pass to another data grid-view accordingly?

i have tried passing to another textbox but it is not ideal

if (tableListBox.SelectedIndex == 2)                
{
  foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
  {
      int counter = 0;
      ID = dr.Cells[1].Value.ToString();
      while (counter < dataGridView1.Columns.Count)
      {
         ColumnsHeader = dataGridView1.Columns[counter].HeaderText;
         CellsValue = dr.Cells[counter].Value.ToString();
         pass += ColumnsHeader + "        " + CellsValue + "  " + "\n";
         counter++;
      }
   Form4 form4 = new Form4(pass, dr.Cells["Deposition Date"].Value.ToString(), ID, tableListBox.SelectedIndex, dr.Cells["Deposition Date"].Value.ToString());
   form4.Show();
   break;
   }  
}

Upvotes: 1

Views: 2043

Answers (2)

Edge Yewe
Edge Yewe

Reputation: 65

            List<object> sendingList = new List<object>();
            foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
            {
                 Form4 form4 = new Form4(richtextbox.text)
                 Form4.Show();
            }

this will pass the result from current form to next form

Upvotes: -2

VahiD
VahiD

Reputation: 1064

If your GridView has bound to an object, you can get bound object and send it to form2, because it has all information you need.

if (tableListBox.SelectedIndex == 2)
            {
                List<object> sendingList = new List<object>();
                foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
                {
                    int counter = 0;
                    sendingList.Add(dr.DataBoundItem);

                } 
                    Form4 form4 = new Form4(sendingList);
                    form4.Show();
                    break; 
            }

I assume that your Form4 class has a constructor with a List argument

Upvotes: 1

Related Questions