PDA

Click to See Complete Forum and Search --> : Getting Variable Value from One form to another


everard
Mar 22nd, 2007, 09:55 AM
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?

Iron Skull
Mar 22nd, 2007, 02:19 PM
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..)

everard
Mar 22nd, 2007, 04:34 PM
I tried that already but it doesn't work.

Harsh Gupta
Mar 22nd, 2007, 05:30 PM
Everything in .Net is an object and derives from System.Object. So, even a form is an object.

//Create object of Form2
Form2 frm = new Form2();

//Now access that variable and set it's value
frm.Variable = set_value

JenniferBabe
Mar 22nd, 2007, 10:12 PM
Everything in .Net is an object and derives from System.Object. So, even a form is an object.

//Create object of Form2
Form2 frm = new Form2();

//Now access that variable and set it's value
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.

RaviIntegra
Mar 23rd, 2007, 04:22 AM
//Create object of Form2
Form2 frm=new Form2();

//Assign the value to Form2 Variable
frm.variablename=value

everard
Mar 23rd, 2007, 08:22 AM
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".

engjulli
Mar 23rd, 2007, 08:47 AM
For hte first question i solwed with global variable, so read for global variables and ask agen!

everard
Mar 23rd, 2007, 09:41 AM
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).

JenniferBabe
Mar 28th, 2007, 10:12 AM
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

Romeo
Apr 6th, 2007, 02:14 AM
I guess this is what you're looking for
DataGridView[0, DataGridView.CurrentCell.RowIndex].Value;
0 is the colomnumber