dynamically create datagrid and set its datasource in VB.NET

D

Donna

I try to dynamically add datagrid to my web form in VB.NET. But when I

try to setup the datasource, the DATASOURCE is not the property of
datagrid. Anybody know the reason? Here is my code:

Public Sub CreateGrid()
'declare a new datagrid and set properties
Dim DataGrid1 As New DataGrid()
DataGrid1.ShowHeader = True
DataGrid1.AutoGenerateColumns = False
DataGrid1.SelectedItemStyle.BackColor = Color.Yellow

'add bound columns to the datagrid
Dim datagridcol As New BoundColumn()
datagridcol.HeaderText = "Last Name"
datagridcol.DataField = "lastname"
DataGrid1.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "First Name"
datagridcol.DataField = "firstname"
DataGrid1.Columns.Add(datagridcol)

'bind datagrid
'*************************************
'I will get error in here and tell me that
'datasource is not the property of DataGrid1
DataGrid1.DataSource = GetDataSet()
DataGrid1.DataBind()

'add datagrid to the page
Page.Controls(1).Controls.Add(DataGrid1)

End Sub

I tried the same thing in C#, the DATASOURCE is the property of
datagrid and it works great.


thanks for any help
 
D

Donna

I found that I have another class also called datagrid which caused
that there is no datasource property. I should use the full name like
system.web.ui.controls.datagrid to declare it.
 
E

Elton Wang

I'm glad you finally figure it out yourself.
-----Original Message-----
I found that I have another class also called datagrid which caused
that there is no datasource property. I should use the full name like
system.web.ui.controls.datagrid to declare it.

VB.NET. But when
I

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top