How to pass value from one form to other from in C#.NET windows application?
In this article I am going to show you how to pass values between two VB.NET forms. This technique may require sometimes when you develop your windows application. This is a simple code just by using C# syntax.
How to pass values between two VB.NET forms
Let's start with a small example. First we will load the data in the datagridview control. Then when user click on “view selected record details" button we will bind values in another form text box control and display to user.
Form 1 Server side
namespace AssignOtherFormTxtCtrlvalue
{
public partial class Form1 : Form
{
int selrow;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("EMP.No");
dt.Columns.Add("EMPNAME");
dt.Columns.Add("SALARY");
dr = dt.NewRow();
dr["EMP.No"] = "101";
dr["EMPNAME"] = "John";
dr["SALARY"] = "12000";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EMP.No"] = "102";
dr["EMPNAME"] = "Mike";
dr["SALARY"] = "13000";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EMP.No"] = "103";
dr["EMPNAME"] = "Andrew";
dr["SALARY"] = "8000";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EMP.No"] = "104";
dr["EMPNAME"] = "James";
dr["SALARY"] = "9600";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EMP.No"] = "105";
dr["EMPNAME"] = "Mike";
dr["SALARY"] = "20000";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
selrow = e.RowIndex;
}
private void button1_Click(object sender, EventArgs e)
{
//Create instance of second form
Form2 obj = new Form2();
//Assign Employee No from grid view to second form textbox
obj.Controls["TextBox1"].Text = dataGridView1.Rows[selrow].Cells[0].Value.ToString();
//Assign Employee Name from grid view to second form textbox
obj.Controls["TextBox2"].Text = dataGridView1.Rows[selrow].Cells[1].Value.ToString();
//Assign Employee Salary from grid view to second form textbox
obj.Controls["TextBox3"].Text = dataGridView1.Rows[selrow].Cells[2].Value.ToString();
//Open Details form
obj.ShowDialog();
}
}
}
Output
Source Code Detail:
Here with I have attached source code download it and try to pass values between two forms.
Front End : Windows form design
Code Behind : C#
Conclusion:
I hope this article help to know pass values between two form controls.
hi super example its very use ful