connectseekers.com

Sunday, 26 June 2016

Display DataGridView Selected Row Values To Second From Using C# In Windows Application

6/26/2016 - By Pranav Singh 0


This article will show you how you can transfer/move/pass the value of the selected row of a datagridview to form2 using C#.net in windows application. In this it has shown that datagridview row value passing from form 1 to form 2 using C#.

So for this article first we will create a new windows application and add a two forms one for gridview and other for displaying the selected value. So on form1 add the add a datagridview.


 Now add the below code into the form code behind.

private void Form3_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = GetDataFromDataBase();
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// Get you data
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataFromDataBase()
        {
            DataTable dt = new DataTable();
            dt.Clear();
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Population");
            dt.Columns.Add("Code");

            DataRow dataRow1 = dt.NewRow();
            dataRow1["Id"] = 1;
            dataRow1["Name"] = "India";
            dataRow1["Population"] = "125 Cr";
            dataRow1["Code"] = "IN";
            dt.Rows.Add(dataRow1);

            DataRow dataRow2 = dt.NewRow();
            dataRow2["Id"] = 2;
            dataRow2["Name"] = "Pakistan";
            dataRow2["Population"] = "50 Cr";
            dataRow2["Code"] = "PK";
            dt.Rows.Add(dataRow2);

            DataRow dataRow3 = dt.NewRow();
            dataRow3["Id"] = 3;
            dataRow3["Name"] = "United States";
            dataRow3["Population"] = "25 Cr";
            dataRow3["Code"] = "US";
            dt.Rows.Add(dataRow3);
            return dt;
        }

In above code I have bind the datagridview to the datatable on form load. Now we will generate the SelectionChanged of the datageidview and add the below code in this event.

  private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;
            foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells)
            {
                cell = selectedCell;
                break;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                string form4_Id = row.Cells["ID"].Value.ToString();
                string form4_Name = row.Cells["Name"].Value.ToString();
                string form4_Population = row.Cells["Population"].Value.ToString();
                string form4_Code = row.Cells["Code"].Value.ToString();
                Form4 objform4 = new Form4();
                objform4.Id = form4_Id;
                objform4.Name = form4_Name;
                objform4.Population = form4_Population;
                objform4.Code = form4_Code;
                objform4.Show();
            }
        }

In above code I am accessing the value grid cells by their bind name, and assigning it to public variable of form 2.

 
Now check he code for form2.

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

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        public string Id = "";
        public string Name = "";
        public string Population = "";
        public string Code = "";
        private void Form4_Load(object sender, EventArgs e)
        {
            lblId.Text = Id;
            lblName.Text = Name;
            lblPopulation.Text = Population;
            lblCode.Text = Code;
        }
    }
}

In above code i have just assign the public variable value to the control on page load. Now we have done run the application to check the output.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top