Friday 7 March 2014

How to transfer data from one form to another form in c# in window application.

I have a datagridview a form which have name "ParentForm".The datagridview have the column name "Name and Age".
Now I want to transfer(retrieve) the column name "id" into the "ChildForm" in window apps. 

There change the value and save it. that value Update in "ParentForm" DataGridview.



Parent Form Design






Parent Form Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormsDemoProject
{
    public partial class ParentForm : Form
    {
        DataTable dtStudentTable;
        string studentName;
        string studentAge;
       
        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
        public string StudentAge
        {
            get { return studentAge; }
            set { studentAge = value; }
        }

        public ParentForm()
        {
            InitializeComponent();
            dtStudentTable = new DataTable();
            dtStudentTable.Columns.Add("Student Name");
            dtStudentTable.Columns.Add("Age");
            StudentGridView.DataSource = dtStudentTable;
        }
       
        private void btnAdd_Click(object sender, EventArgs e)
        {
            DataRow drStudentRow = dtStudentTable.NewRow();
            drStudentRow["Student Name"] = txtStudentName.Text;
            drStudentRow["Age"] = txtStudentAge.Text;
            dtStudentTable.Rows.Add(drStudentRow);
            StudentGridView.DataSource = dtStudentTable;
            txtStudentName.Clear();
            txtStudentAge.Clear();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            StudentName = StudentGridView.SelectedRows[0].Cells[0].Value.ToString();
            StudentAge = StudentGridView.SelectedRows[0].Cells[1].Value.ToString();
            ChildForm childForm = new ChildForm();
            childForm.StudentName = StudentName;
            childForm.StudentAge = StudentAge;
            childForm.ParentFormReference = this;
            childForm.InitializeValues();
            childForm.ShowDialog();
        }

        public void UpdateGrid()
        {
            StudentGridView.SelectedRows[0].Cells[0].Value = StudentName;
            StudentGridView.SelectedRows[0].Cells[1].Value = StudentAge;
        }
    }
}


ChildForm Design


Child Form Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormsDemoProject
{
    public partial class ChildForm : Form
    {
        ParentForm parentFormRef;
        string studentName;
        string studentAge;

        public ChildForm()
        {
            InitializeComponent();
        }

        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
        public string StudentAge
        {
            get { return studentAge; }
            set { studentAge = value; }
        }

        public ParentForm ParentFormReference
        {
            get { return parentFormRef; }
            set { parentFormRef = value; }
        }

        public void InitializeValues()
        {
            txtStudentName.Text = StudentName;
            txtStudentAge.Text = StudentAge;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            parentFormRef.StudentName = txtStudentName.Text;
            parentFormRef.StudentAge = txtStudentAge.Text;
            parentFormRef.UpdateGrid();
            this.Close();
        }
    }
}


Output :