Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically declare Datagrid using variable? 1

Status
Not open for further replies.

rborel

MIS
Dec 3, 2002
29
US
Is it at all possible to dynamically create a Datagrid using a variable as it's name? I have tried and am either not doing something correct or it cannot be done. Please help. I have tried the following with no success.


Private Sub brunTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brunTest.Click
Dim i As Integer
Dim strA As String
Dim strAi As String
strA = "dgDataGrid"
For i = 0 To 10
strAi = strA & i
Dim strAi as new Datagrid
Response.Write(strAi)
Response.Write("<br>")
Next
End Sub
 
You'll have to set the "name" of the DataGrid by setting it's ID attribute e.g.
Code:
Dim newDG as New DataGrid
newDG.ID = myVariable


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
c,
So, I can create multiple datagrids by Dim'ing only newDG?

More of the story. . .

I have an array that I would like to be able to read out and only create the datagrid if there is data from a query to fill the datagrid.

Thanks for the information
 
Yes, sort of. e.g.
Code:
            Dim newDG As DataGrid
            For i As Integer = 0 To 10
                newDG = New DataGrid
                newDG.ID = "myDG" & i
                Me.Controls.Add(newDG)
            Next


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 316: For i = 0 To 10
Line 317: Dim newDG As DataGrid
Line 318: newDG.ID = "myDG" & i
Line 319: Me.Controls.Add(newDG)
Line 320: Response.Write(strAi)



Code:
    Private Sub brunTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brunTest.Click
        Dim i As Integer
        Dim strA As String
        Dim strAi As String
        strA = "dgDataGrid"
        For i = 0 To 10
            Dim newDG As DataGrid
            newDG.ID = "myDG" & i
            Me.Controls.Add(newDG)
            Response.Write(strAi)
            Response.Write("<br>")
        Next
    End Sub
 
Yes, you are declaring the DataGrid inside the loop whereas I declared mine outside.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
You need to declare a NEW datagrid

Dim newDG as New DataGrid
 
My bad, thanks for the help. Now I just need to figure how to integrate it into the rest of my code.

thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top