لینک آموزش
با فرض اینکه در فرم اول یک دیتاگریدویو و یک دکمه و در فرم دوم یک دیتاگریدویو داشته باشید و بخوایهد با کلیک بر روی دکمه سطرهای انتخاب شده دیتاگریدویو فرم اول به دیتاگریدویو فرم دوم منتقل شوند باید در رویداد کلیک دکمه در فرم اول کدهای زیر :
private void button1_Click(object sender, EventArgs e)
{
if (this.dataGridView1.Rows.Count > 0)
{
List<DataGridViewRow> selectedRows = new List<DataGridViewRow>();
for (int i = this.dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
{
selectedRows.Add(this.dataGridView1.SelectedRows[i]);
}
if (selectedRows.Count > 0)
{
Form2 form2 = new Form2(selectedRows);
form2.ShowDialog();
}
}
}
و در فرم دوم هم کدهای زیر را بنویسید :
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace TransferValueDGV
{
public partial class Form2 : Form
{
private List<DataGridViewRow> _selectedRows;
public Form2(List<DataGridViewRow> selectedRows)
{
InitializeComponent();
this._selectedRows = selectedRows;
}
private void Form2_Load(object sender, EventArgs e)
{
if (this._selectedRows != null &&
this._selectedRows.Count > 0)
{
for (int i = 0; i < this._selectedRows.Count; i++)
{
this.dataGridView1.Rows.Add(this._selectedRows[i].Cells[0].Value.ToString(),
this._selectedRows[i].Cells[1].Value.ToString());
}
}
}
}
}
برنامه را اجرا و با انتخاب چند سر و زدن دکمه نتیجه را مشاهده کنید :
لینک آموزش