Click here to Skip to main content
11,417,972 members (42,752 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET C#3.5
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 12-Oct-11 22:21pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.....
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

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

//FormOne FormTwo 
frmtwo = new FormTwo(txtUserId.Text);
frmTwo.Show()
  Permalink  
Comments
suresh lohare at 17-Jul-12 2:54am
   
Thanks
it is working
Member 3080470 at 7-Dec-12 2:33am
   
Dear Sir
How to create single instance of forms which are created as instances.
Thanks
ram
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:

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

In Form2:

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

    }
}
  Permalink  

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

  Print Answers RSS
0 Sascha Lefévre 340
1 Abhinav S 339
2 OriginalGriff 315
3 Sergey Alexandrovich Kryukov 245
4 deepankarbhatnagar 121
0 Sergey Alexandrovich Kryukov 9,345
1 OriginalGriff 7,712
2 Maciej Los 3,758
3 Abhinav S 3,601
4 Sascha Lefévre 3,198


Advertise | Privacy | Mobile
Web04 | 2.8.150427.4 | Last Updated 13 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100