Transfer DataGrid Row to Another Empty DataGrid

This is the article about how to transfer row of one DataGrid to another DataGrid one by one on double click event of an DataGrid. How You will do it. This coding will explain you .

For this application you need two DataGrid and two Command Button

First button Name is Submit Button

Second Button Name is Exit Button.

Second grid Name is da_SpecifiedGrid.

------------------Code-----------------------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Reflection;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

 

namespace WindowsApplication3

{

          /// <summary>

          /// Summary description for Form1.

          /// </summary>

          public class FrmPractice : System.Windows.Forms.Form

          {

                   private System.Windows.Forms.DataGrid dataGrid1;

                   private System.Windows.Forms.Button btn_Click;

                   private System.Windows.Forms.DataGrid da_SpecificRecords;

                   private System.Data.DataTable dtSecondGrid = new System.Data.DataTable();

 

                   /// <summary>

                   /// Required designer variable.

                   /// </summary>

                   private System.ComponentModel.Container components = null;

                   private System.Windows.Forms.Button btn_Exit;

                   public FrmPractice()

                   {

                             //

                             // Required for Windows Form Designer support

                             //

                             InitializeComponent();

                             //

                             // TODO: Add any constructor code after InitializeComponent call

                             //

                   }

 

                   /// <summary>

                   /// Clean up any resources being used.

                   /// </summary>

                   protected override void Dispose( bool disposing )

                   {

                             if( disposing )

                             {

                                      if (components != null)

                                      {

                                                components.Dispose();

                                      }

                             }

                             base.Dispose( disposing );

                   }

 

                   #region Windows Form Designer generated code

 

                   /// <summary>

                   /// Required method for Designer support - do not modify

                   /// the contents of this method with the code editor.

                   /// </summary>

 

                   private void InitializeComponent()

                   {

                             this.dataGrid1 = new System.Windows.Forms.DataGrid();

                             this.btn_Click = new System.Windows.Forms.Button();

                             this.da_SpecificRecords = new System.Windows.Forms.DataGrid();

                             this.btn_Exit = new System.Windows.Forms.Button();

                             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();

                             ((System.ComponentModel.ISupportInitialize)(this.da_SpecificRecords)).BeginInit();

                             this.SuspendLayout();

                             //

                             // dataGrid1

                             //

                             this.dataGrid1.DataMember = "";

                             this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;

                             this.dataGrid1.Location = new System.Drawing.Point(32, 48);

                             this.dataGrid1.Name = "dataGrid1";

                             this.dataGrid1.Size = new System.Drawing.Size(440, 152);

                             this.dataGrid1.TabIndex = 0;

                             this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick);

                             this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);

                             //

                             // btn_Click

                             //

                             this.btn_Click.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(128)));

                             this.btn_Click.Location = new System.Drawing.Point(48, 224);

                             this.btn_Click.Name = "btn_Click";

                             this.btn_Click.Size = new System.Drawing.Size(96, 23);

                             this.btn_Click.TabIndex = 1;

                             this.btn_Click.Text = "Show Records";

                             this.btn_Click.Click += new System.EventHandler(this.btn_Click_Click);

                             //

                             // da_SpecificRecords

                             //

                             this.da_SpecificRecords.AllowDrop = true;

                             this.da_SpecificRecords.DataMember = "";

                             this.da_SpecificRecords.HeaderForeColor = System.Drawing.SystemColors.ControlText;

                             this.da_SpecificRecords.Location = new System.Drawing.Point(32, 288);

                             this.da_SpecificRecords.Name = "da_SpecificRecords";

                             this.da_SpecificRecords.Size = new System.Drawing.Size(440, 128);

                             this.da_SpecificRecords.TabIndex = 2;

                             //

                             // btn_Exit

                             //

                             this.btn_Exit.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(128)));

                             this.btn_Exit.Location = new System.Drawing.Point(376, 224);

                             this.btn_Exit.Name = "btn_Exit";

                             this.btn_Exit.Size = new System.Drawing.Size(96, 23);

                             this.btn_Exit.TabIndex = 3;

                             this.btn_Exit.Text = "Exit";

                             this.btn_Exit.Click += new System.EventHandler(this.btn_Exit_Click);

                             //

                             // FrmPractice

                             //

                             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

                             this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(128)));

                             this.ClientSize = new System.Drawing.Size(504, 510);

                             this.Controls.Add(this.btn_Exit);

                             this.Controls.Add(this.da_SpecificRecords);

                             this.Controls.Add(this.btn_Click);

                             this.Controls.Add(this.dataGrid1);

                             this.Name = "FrmPractice";

                             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

                             this.Text = "Practice";

                             this.Load += new System.EventHandler(this.FrmPractice_Load);

                             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();

                             ((System.ComponentModel.ISupportInitialize)(this.da_SpecificRecords)).EndInit();

                             this.ResumeLayout(false);

                   }

                    #endregion

 

                   /// <summary>

                   /// The main entry point for the application.

                   /// </summary>

                   [STAThread]

                   static void Main()

                   {

                             Application.Run(new FrmPractice());

                   }

 

                   private void btn_Click_Click(object sender, System.EventArgs e)

                   {

                             SqlConnection oConn = new SqlConnection("Server=CTX91;uid=sa;pwd=sa;Database=Northwind");

                             string sql = "select * from Customers";

                             SqlDataAdapter DA = new SqlDataAdapter(sql,oConn);

                             DataSet DS = new DataSet();

                             DA.Fill(DS,"Customers");

                             dataGrid1.DataSource = DS.Tables["Customers"].DefaultView;

                   }

 

                   private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)

                   {

                   }

 

                   private void dataGrid1_DoubleClick(object sender, System.EventArgs e)

                   {

                             try

                             {

                                      string str = "";

                                      str = dataGrid1[dataGrid1.CurrentRowIndex,0].ToString();

                                      SqlConnection oConn = new SqlConnection("Server=CTX91;uid=sa;pwd=sa;Database=Northwind");

                                      oConn.Open();

                                      string sql = "select * from Customers where CustomerID = '" + str + "'";

                                      SqlDataAdapter DA = new SqlDataAdapter(sql,oConn);

                                      DataSet DS = new DataSet();

                                      DA.Fill(DS,"Customers");

                                      if(dtSecondGrid.Rows.Count<=0)

                                      {

                                                dtSecondGrid = DS.Tables[0];

                                      }

                                      else

                                      {

                                                foreach(DataRow myRow in DS.Tables[0].Rows)

                                                {

                                                          dtSecondGrid.ImportRow(myRow);

                                                          break;

                                                }

                                      }

                                      da_SpecificRecords.DataSource = dtSecondGrid;

                             }

                             catch(System.Exception E)

                             {

                                      MessageBox.Show(E.Message.ToString());

                             }

 

                   }

                   private void FrmPractice_Load(object sender, System.EventArgs e)

                   {

                   }

 

                   private void btn_Exit_Click(object sender, System.EventArgs e)

                   {

                             Application.Exit();

                   }

          }

}

Up Next
    Ebook Download
    View all
    Learn
    View all