Accessing datagridview data at class level

phudgens

Member
Joined
Nov 19, 2013
Messages
19
Programming Experience
10+
I have a C# GUI based program that has all its coding in the first (button1_click) level - it is working well. I am re-writing this to be more object oriented, and have created a class with a constructor. I can send in the textboxes, comboboxes, checkboxes, etc via the constructor, but have not found a way to send in the datagridview. When I try to access the datagridview at the class level, Visual Studio does not recognize any of it's named members (columns). Is there a way to do this? I am using Visual Studio Express 2013.

Thanks,
Paul Hudgens
Denver
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
814
Location
Norway
Programming Experience
10+
What you are referring to is not a named member of Datagridview, but a form variable that holds the control/component object reference. When you add controls in designer a private member variable (class field) is added in generated code, that is also the case for each column, the generated code the proceeds to add all these column objects to the grids Columns collection. From the Datagridview reference you can get a reference to each column from its Columns collection.

For regular controls/components there is a Modifiers property that for simplicity could allow you to expose these member variables publicly, you could then pass the form reference and from there access its "named members". That not the case for the columns in Datagridview though, to do that you would have to manually add a public property that returned the column.
 

phudgens

Member
Joined
Nov 19, 2013
Messages
19
Programming Experience
10+
Accessing Form1 textboxes at the class level

I did get it to work using the named column after passing the datagridview to the constructor. Thanks very much.

I'm hoping that there might be a way to access my textboxes (15 of them), comboboxes, and checkboxes directly from my class rather than having to send them all in via the constructor. The following seems to come the closest to what I'm trying to do:

public class TBoxFunctions
{
public static void RetrieveTBoxes()
{
Form1 MyForm1;
MyForm1 = (Form1)System.Windows.Forms.Control.ControlCollection;
TextBox TBox1 = MyForm1.textBox1;
TextBox TBox2 = MyForm1.textBox2;
...
}
}

Then invoke it with:

TBoxFunctions.RetrieveTBoxes();

Visual Studio does recognize "textBox1" , but regardless of what I try, I can't get my definition of MyForm1 past the compiler. Can anyone see the problem?

THanks,
Paul Hudgens
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
814
Location
Norway
Programming Experience
10+
It seems that is what I explained in previous post:
For regular controls/components there is a Modifiers property that for simplicity could allow you to expose these member variables publicly, you could then pass the form reference and from there access its "named members".
 

phudgens

Member
Joined
Nov 19, 2013
Messages
19
Programming Experience
10+
Is there a precise amount of time (in hours) that you think I should spend trying to figure out what the precise syntax should be to do this? I have spent about 4 now trying to get something past the compiler. As with your original post, what you provided was of no help - I figured it out on my own. Do you know of any other C# forums that might have helpful people working for them?

And yes - I'll cancel my membership to your forum immediately.
 
Top Bottom