DataGridview As Variable?

Fedaykin

Active member
Joined
Mar 20, 2013
Messages
30
Programming Experience
Beginner
This might be simple, but it eludes me. I need to assign a DataGridView to a variable, but i'm getting an intellisense error when trying to use it:

TestGrid is not a member of 'myForm'

VB.NET:
Dim TestGrid As DataGridView = DataGridView1

Me.TestGrid.Sort(TestGrid.Columns("ShipDate"), ListSortDirection.Descending)
 
Opps! I forgot to put 'New' in front of DataGridView:

VB.NET:
Dim TestGrid As New DataGridView = DataGridView1

Me.TestGrid.Sort(TestGrid.Columns("ShipDate"), ListSortDirection.Descending)
 
You don't and can't put New in that context. New is to create a new object. Do you want to create a new object? Obviously not, so don't use it. The issue is just as the error message says: TestGrid is not a member of the form. You use Me to refer to the current instance of the current type and your TestGrid variable is not a member of that object; it's a local variable. You don't use Me to qualify local variables. Presumably DataGridView1 is a member variable so you could qualify it with Me. That begs the question though, if you already have the DataGridView1 variable, do you actually need the TestGrid variable? Maybe you do but maybe you don't. Regardless, use Me for members only. You don't qualify local variables with anything because they aren't members of anything.
 
Back
Top