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
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.


Attachments

  • Source Code (43591-191842-AssignOtherFormTxtCtrlvalue.rar)
  • Comments

    Guest Author: vimal28 Mar 2012

    hi super example its very use ful

    Author: saleem v29 Oct 2012 Member Level: Bronze   Points : 0

    its very useful.Here values loads to text boxes of second.How load values to compo box of second using c# code...please help

    Author: saleem v30 Oct 2012 Member Level: Bronze   Points : 0

    its very useful.Here values loads to text boxes of second.How load values to compo box of second using c# code...please help

    Author: Ravindran30 Oct 2012 Member Level: Gold   Points : 1

    try like this


    //Add combo box value
    ComboBox c1 = (ComboBox)obj.Controls["comboBox1"]; //change control id as per code
    c1.Items.Add("Red");
    c1.Items.Add("green");
    c1.Items.Add("Blue");

    Author: Ram Prasad06 Apr 2014 Member Level: Gold   Points : 0

    How to pass datagridview values to another form datagridview based on specific row using c#.net. Please help..

    Author: Ram Prasad07 Apr 2014 Member Level: Gold   Points : 0

    How to pass datagridview values to another form datagridview based on specific row using c#.net. Please help me...

    Author: Nirav Lalan17 Jun 2014 Member Level: Gold   Points : 2

    HI,

    I wrote this code as you suggest in this forum.
    BillEntry be = new BillEntry();
    be.Controls["TextBoxBank"].Text = BankGridView.Rows[selrow].Cells[0].Value.ToString();
    this.Close();
    be.ShowDialog();

    but the TextBoxBank is not getting filled with value.

    Author: Ram Prasad21 Sep 2014 Member Level: Gold   Points : 3

    Hi,

    I am getting below error message while transferring the values to DataGridViewTextBoxColumn:

    "object reference not set to an instance of an object"

    frmCopyOneRowToAnotherRow obj = new frmCopyOneRowToAnotherRow();
    obj.Controls["txtDgvColor"].Text = dgvDailyProdEntry.Rows[selrow].Cells[4].Value.ToString();
    obj.ShowDialog();

    Here txtDgvColor is the DataGridViewTextBoxColumn.


    Note: I am not getting any error while transferring data into plain textboxes.

    Author: Nirav Lalan09 Apr 2015 Member Level: Gold   Points : 2

    Hello, Imran Ali,

    Refer the below link. I have posted one resource. It is working fine.
    http://www.dotnetspider.com/resources/45732-pass-value-from-one-form-to-other-from-in-C-NET-windows-application.aspx

    If you want this code in VB.NET just convert the code and it will start working.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: