انتقال یک سطر از یک دیتاگرید به دیتاگرید دیگر

برای انتقال یک سطر از یک دیتاگرید به دیتاگرید دیگر مانند شکل زیر ، ابتدا دو کنترل Datagridview به فرم اضافه کنید

move-a-row-from-one-datagrid-to-another-csharp-01

سپس مراحل زیر را برای اضافه کردن ستون به Datagridview اول طی کنید :

move-a-row-from-one-datagrid-to-another-csharp-02

move-a-row-from-one-datagrid-to-another-csharp-03

move-a-row-from-one-datagrid-to-another-csharp-04

move-a-row-from-one-datagrid-to-another-csharp-05

move-a-row-from-one-datagrid-to-another-csharp-06
در انتها خاصیت SelectionMode از Datagridview اول را برابر FullRowSelect قرار دهید :

move-a-row-from-one-datagrid-to-another-csharp-07 
سپس کد زیر را به رویداد های مربوطه اضافه کنید :

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Add(new object[] { "siavash","ebrahimi","20"});
            this.dataGridView1.Rows.Add(new object[] { "younes", "ebrahimi", "30" });
            this.dataGridView1.Rows.Add(new object[] { "siavsh", "amraie", "31" });
            this.dataGridView1.Rows.Add(new object[] { "yahya", "ebrahimi", "30" });
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                if (this.dataGridView2.Columns.Count == 0)
                {
                    this.dataGridView2.Columns.Add("Column1", "Column1");
                    this.dataGridView2.Columns.Add("Column2", "Column2");
                    this.dataGridView2.Columns.Add("Column3", "Column3");
                }
                string name = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
                string family = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
                string age = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
                this.dataGridView2.Rows.Add(new object[] { name, family, age });
            }            

        }
    }
}

حال برنامه را اجرا و بر روی یکی از سطر های کلیک کنید و نتیحه را مشاهده نمایید.
move-a-row-from-one-datagrid-to-another-csharp-08