There are 2 forms. On form 1, there is a table and a button, by pressing which the second datagridview should be filled. When both grids are on the same form, then everything works fine. And how can I make one grid and one button on form 1 and the second form with grid opens on pressing the button and it fills in the second form?

private void button2_Click(object sender, EventArgs e) { Square(); } public void Square() { int n = t1DataGridView.SelectedRows.Count; string[] labels = new string[n]; DataTable dt = new DataTable(); for (int i = 0; i < n; i++) { labels[i] = Convert.ToString(t1DataGridView.SelectedRows[i].Cells[0].Value); dt.Columns.Add(labels[i]); dt.Rows.Add(); } dataGridView1.DataSource = dt; for (int i = 0; i < n; i++) { dataGridView1.Rows[i].HeaderCell.Value = labels[i]; } } 
  • one
    Immediately I can offer two ways. The first is to remember all the data that needs to be copied, and transfer them to the constructor of the second form. The second is to make the grid on the second form public and contact it directly. - Geslot pm

1 answer 1

First you need to create a 2nd form. Ctrl+Shift+A -> Windows Forms -> Π€ΠΎΡ€ΠΌΠ° Windows Forms . Then you need to add a dataGridView control to the 2nd form. You can add in the form designer, you can programmatically. Immediately make the control dataGridView open so that you can access it from the class of the 1st form. To do this, in the properties of the control, we make the parameter Modifiers -> Public . Then we set up the processing of the button we need.

 private void button1_Click(object sender, EventArgs e) { // создаСм ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ 2ΠΎΠΉ Ρ„ΠΎΡ€ΠΌΡ‹ Form2 f = new Form2(); //Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΎΠ±Ρ€Π°Ρ‰Π°Ρ‚ΡŒΡΡ ΠΊ ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Ρƒ dataGridView //ΠΌΠΎΠΆΠ½ΠΎ Ρ‡Π΅Ρ€Π΅Π· ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Ρ„ΠΎΡ€ΠΌΡ‹ Ρ‚Π°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ //ΠΊ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Ρƒ, Π·Π°Π΄Π°Π΄ΠΈΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€Π½ΠΎΡΡ‚ΡŒ dataGridView f.dataGridView1.ColumnCount = 1; f.dataGridView1.RowCount = n; //ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ Ρ„ΠΎΡ€ΠΌΡƒ f.ShowDialog(); }