Results 1 to 11 of 11

Thread: Getting Variable Value from One form to another

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Getting Variable Value from One form to another

    I have a program with multiple form in C#. I used a DataGridView to display a list of Students from my database. Whenever the user clicks on the datagrid, it automatically saves the name of the selected student on a string variable.

    My question now is, how can I get the value of that variable from Form1 to Form2?
    To become a PROFESSIONAL,
    Start from SCRATCH...

  2. #2
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    Re: Getting Variable Value from One form to another

    I almost never use C# but this might help.
    Try in form1:
    form2.Variable

    That might work (it does in Vb and C# is C++ combined with vb so I guess It would work..)

    506C65617365205261746520506F7374732E2E2E

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting Variable Value from One form to another

    I tried that already but it doesn't work.
    To become a PROFESSIONAL,
    Start from SCRATCH...

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Getting Variable Value from One form to another

    Everything in .Net is an object and derives from System.Object. So, even a form is an object.

    c# Code:
    1. //Create object of Form2
    2. Form2 frm = new Form2();
    3.  
    4. //Now access that variable and set it's value
    5. frm.Variable = set_value
    Show Appreciation. Rate Posts.

  5. #5
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Getting Variable Value from One form to another

    Quote Originally Posted by Harsh Gupta
    Everything in .Net is an object and derives from System.Object. So, even a form is an object.

    c# Code:
    1. //Create object of Form2
    2. Form2 frm = new Form2();
    3.  
    4. //Now access that variable and set it's value
    5. frm.Variable = set_value

    One note is the in Form2, the variable "Variable" must be set to public or else an error will be generated.

  6. #6
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: Getting Variable Value from One form to another

    vb Code:
    1. //Create object of Form2
    2. Form2 frm=new Form2();
    3.  
    4. //Assign the value to Form2 Variable
    5. frm.variablename=value

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting Variable Value from One form to another

    Another question, how can I get a specific value from a data column? For example, I have a row for student's last name, first name, middle name, course, and year level. Then when the user clicks on a single student record, the last name of the selected student will automatically assigned to variable "StudLastName".
    To become a PROFESSIONAL,
    Start from SCRATCH...

  8. #8
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Getting Variable Value from One form to another

    For hte first question i solwed with global variable, so read for global variables and ask agen!

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting Variable Value from One form to another

    Sorry, my question was not detailed. What I'm trying to ask is how can I get a specific value from a datagridview. Getting a specific data from a student (ex. LastName).
    To become a PROFESSIONAL,
    Start from SCRATCH...

  10. #10
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Getting Variable Value from One form to another

    ok you want to click on a row and retrive the student last name correct? That is a very simple method for a data table structure, I cant' rememebr it right now but it's simple, you just got to pass the name of the column to pull out what you want. From what I see, its mostly string manipulation that might solve your problem.

    If you can't find the method, please reply, if so I will find some time to look up the method for you.

    Jennifer

  11. #11
    Junior Member
    Join Date
    Aug 2005
    Posts
    30

    Re: Getting Variable Value from One form to another

    I guess this is what you're looking for
    Code:
    DataGridView[0, DataGridView.CurrentCell.RowIndex].Value;
    0 is the colomnumber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width