Click here to Skip to main content
15,943,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview a form which have name "Form1".The datagridview have the column name "id".
Now I want to transfer(retrieve) the column name "id" into the "Form2" in window apps.
So what can I do because we cannot use the "Session" in window apps.
So give the answer.
Posted

It depends on what you are doing.
By the sound of it, you are opening a new instance of Form2 specifically to show data related to to the selected id.
If so, then the best way is to change the constructor of Form2 to accept an id value, and process it as it is constructed.

There are other ways, but as I say - it depends on what you are doing.
 
Share this answer
 
this is the article will completely explaining you various ways available for pass data between forms.
as i was facing same issue when i was newbie to Visual C#.net and that article helps me lot.
i hope it will helps to you also.
Thanks.....
 
Share this answer
 
v3
Comments
Umesh AP 23-Jul-15 6:35am    
Thanks Pritesh for sharing link.
Data transfer methods are much easier in ASP.NET (web application) than in C#.NET (Window Based application).
ASP.NET uses their own stateManagement technique

Unlike VB.NET in C#.NET we can not access all forms with it's all controls and values directly.

it can be done using following ways

1. Declear a public veriable at formTwo, see the below code
C#
//FormTwo
public string szUserId;
onForm_Load()
{
  Label.Text = szUserId;
}

now show FormTwo on click on FormOne
By creating object of FormTwo, we can Access global veriable and assign values to it

C#
FormOne_Button1_Click()
{   
//create a object of formTwo
   FormTwo frmtwo = new FormTwo();
//Access global veriable and assign values to it
  frmtwo.szUserId = txtUserId.Text;
//Hide formOne
  this.Hide();
//show formtwo
  frmTwo.Show();
}


2. Declear value in constructor on formTwo

C#
//FormTwopublic 
string szUserId;
//in formtwo Constructor assign a value to it's veriable

FormTwo (string Uid)
{     
  Init()
  this.szUserId = Uid
}

onForm_Load()
{
  Label.Text = szUserId;
}



on FormOne while creating Object of FormTwo pass the userid value

C#
//FormOne FormTwo 
frmtwo = new FormTwo(txtUserId.Text);
frmTwo.Show()
 
Share this answer
 
Comments
suresh lohare 17-Jul-12 2:54am    
Thanks
it is working
Member 3080470 7-Dec-12 2:33am    
Dear Sir
How to create single instance of forms which are created as instances.
Thanks
ram
This is a very simple approach:

1. Create an instance of the form (form2)
2. Transfer data to that new form instance
Let's say you have a TextBox control in Form1, and you wish to pass the value from it to Form2 and show Form2 modally.

In Form1:

C#
private void ShowForm2()
{
    string value = id; //id u want to pass
    Form2 newForm = new Form2();
    newForm.TheValue = value;
    newForm.ShowDialog();
}


In Form2:

C#
private string _theValue;
public string TheValue 
{ 
    get
    {
        return _theValue;
    }
    set
    {
        _theValue = value; 
        // do something with _theValue so that it
        // appears in the UI

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900