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#.
Some of my
previous articles are as follows: Bind
DataGridView With Sql Server Table Using C#.Net In Windows Application, Export
GridView Data To Excel Sheet Using C#.Net In Windows Application, How
to Save Record in XML File and Read XML to Display in DataGridview Using C#.net
in Windows Application, How
to Add HyperLink and Retrieve Row Value on Link Click in DataGridView #,
Windows application,
How
to Add Button Control and Retrieve Row Value on Button Click in DataGridView
Using C#.Net, Windows Application, Paging
in DataGridview Using C#.Net In Windows Application, How
to Bind xml to DataGridView Using C#.Net In Windows Application, Bind
And Display Image in a DatagridView Using C#.Net in Windows Application.
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.
0 comments:
Please let me know your view