Solved

How to Create Instance of Existing Object via Casting

Posted on 2014-03-02
6
203 Views
Last Modified: 2014-03-03
I am creating a DataGridView dynamically at run-time, and cant seem to be able to instantiate it as an object during casting in another (Form1) subroutine, so that I can change row labels.


Here is how I define the DataGridView in the Form1 Load procedure:


Dim DataGridView1 As New DataGridView

Open in new window


Later, in a subroutine, when I want to change the row labels (numrows is the number of rows), I can't get the cast to work correctly because the cast won't allow me to use "As New" -- as follows:

Dim DataGridview1 As DataGridView = CType(Me.Controls("DataGridView1"), DataGridView)
For i = 1 To numrows
        DataGridview1.Columns.Add(i, "V" & i)
Next

Open in new window



What is the correct way instantiate as an object a control that was spawned dynamically?
0
Comment
Question by:lep1
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 39900042
what error u got ?
0
 

Author Comment

by:lep1
ID: 39900177
The error is that DataGridView1 is Nothing
0
 
LVL 19

Accepted Solution

by:
Melih SARICA earned 500 total points
ID: 39900217
try

me.controls.find("datagridview1")
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:lep1
ID: 39900343
It worked out that the the syntax

Dim DataGridView1 As New DataGridView

does not add the name "DataGridView1" to the control, so therefore it can't be found in Me.Controls().

I had to use:

DataGridView1.Name="DataGridView1" -->which corrected the problem of not finding the control.

Question: is there a difference between

Dim DataGridView1 As DataGridView

and

Dim DataGridView1 As New DataGridView

in terms of the Name property?
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 39900379
Dim DataGridview1 As DataGridView = CType(Me.Controls("DataGridView1"), DataGridView)

in this line, ur trying to find a control that is already on the page ? or trying to add a new one ?

I guess ur trying to find a newly created one..

so u ave to set a name, other wise ssytem ll gave a name automaticly.
0
 

Author Comment

by:lep1
ID: 39900396
ok, thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Prevent call a sub/function several times when data bind to gridview 21 33
vb.net class 3 21
Generate Unique ID in VB.NET 21 65
Get month and date in a format 4 32
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question