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 : 





Wednesday, 5 March 2014

Zip facility Dot Net 4.5 Features

 Zip facility 

Zip is one of the most accepted archive file formats. Zip format is supported in almost all operating systems with some built-in name.
  • In Windows operating system it’s implemented by the name “Compressed folders”.
  • In MAC OS it’s implemented by the name “Archive utility”.
Now in .NET we did not have built-in support for implementing Zip compression. Many developers where using third party components like “DotnetZip”. In .NET 4.5, the Zip feature is baked in the framework itself, inside the namespaceSystem.IO.Compression.
The first step is you need to reference two namespaces:
  • System.IO.Compression.FileSystem
  • System.IO.Compression
The next step is to import the below two namespaces:
using System.IO.Compression;
If you want to Zip files from a folder you can use the CreateFromDirectory function as shown below.
ZipFile.CreateFromDirectory(@"D:\data",@"D:\data.zip");
If you wish to unzip, you can use the ExtractToDirectory function as shown in the below code.
ZipFile.ExtractToDirectory(@"D:\data.zip", @"D:\data\unzip");

Great .NET Framework 4.5 Features async and await

async and await

This feature has been oversold and every .NET evangelist has talked about it. But this is still my favorite and you will come to know why in a few lines from here.
async and await are markers which mark code positions from where control should resume after a task (thread) completes.
  1. async and await are pair keywords. You cannot use them in a standalone manner.
  2. async is marked on a method. This keyword is just an indicator saying that this method will have the awaitkeyword.
  3. The await keyword marks the position from where the task should resume. So you will always find this keyword in conjunction with Task.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Task_asyncandawait
{
    class Program
    {
        static void Main(string[] args)
        {
            Add(5,6);
            Sub(5, 6);
            Mul(5, 6);
            Console.WriteLine("Main method");
            Console.Read();
        }

        public static async void Add(int a,int b)
        {
           await Task.Run(new Action(LongTask));
           Console.WriteLine("Add Method  =  "+ a + b);
            Console.WriteLine("Add Thread");
        }
        public static async void Sub(int a, int b)
        {
            await Task.Run(new Action(LongTask));
            Console.WriteLine("Sub Method   =   "+  (a - b));
            Console.WriteLine("Sub Thread");
        }
        public static async void Mul(int a, int b)
        {
            await Task.Run(new Action(LongTask));
            Console.WriteLine("Mul Method  =  "+  a * b);
            Console.WriteLine("Mul Thread");
        }
        public static void LongTask()
        {
            Thread.Sleep(10000);
        }
    }
}


Output :



References