Pass value from one form to other from in C#.NET windows application


Mostly in ASP.NET it is easy to move the value one page to another. but in this article i will show you how to pass the DataGridView value from one form to the TextBox in another form using c# windows application.

Hi Coders,

Write this code on form1:

Step 1:Define two global variables


public string code;
int selrow;

Step 2:OK Button click event:

private void ButtonOK_Click(object sender, EventArgs e)
{
code = GridView1.Rows[selrow].Cells[0].FormattedValue.ToString();
form2 fm = new form2();
fm.Controls["TextBox1"].Text = code;
this.Close();// form1 will close here
fm.Controls["TextBox1"].Focus();
}

Step 3:CellDoubleClick event of Gridview

private void GridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
code = GridView1.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
form2 fm = new form2();
fm.Controls["TextBox1"].Text = code;
this.Close();// form1 will close here
fm.Controls["TextBox1"].Focus();
}

Step 4:RowEnter event of Gridview

private void GridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
selrow = e.RowIndex;
}

Write this code on form2:

Step 5:KeyDown event of TextBox1

private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
form1 f = new form1();
f.ShowDialog();
TextBox1.Text = f.code;
}
}

Now you will get the value of 0th cell from gridview1 in one form to TextBox1 on another form using global variable "code"

Nirav Lalan
IT has no ends


Comments



  • 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: