VB.NET dynamically create datagrid and set its datasource

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?

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

thanks for any help
 
E

Elton Wang

It is property of the datagrid. Check out something wrong
in your code.

HTH

Elton Wang
(e-mail address removed)
 
W

WJ


Very Bad. Do not Use!
I tried the same thing in C#, the DATASOURCE is the property of
datagrid and it works great.

Indeed, it is A Great Tool for .Net. On top of that, "c#" is both accepted
as standard by ECMA (European Computer Manufacturers Association) and ISO
beside being a well known musical note (C Major)

John Webb
 
D

Donna

I still feel so surprise that the DATASOURCE is not the property of
datagrid while you dynamically create the databrid.

Elton, I did not see anything wron with my code. Are you sure that
you could have DATASOURCE as property while you DYNAMICALLY create
datagrid?

thanks
 
E

Elton Wang

Hi Donna,

If you still doubt that in VB.NET the datagrid has
DataSource property, please take look in following URLs:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/vbcon/html/vbtskaddingdatagridtowebformspage.asp

http://www.asp.net/Forums/ShowPost.aspx?
tabindex=1&PostID=857462

You can find that many people are using VB.NET for
datagrid. I am using both C# and VB.NET. I don't think
VB.NET is bad.

BTW, why don't you post your code let people see. Here
many people are very helpful.

HTH

Elton Wang
 
D

Donna

Elton,
Here is the code. The url you indicate is not dynamically created
datagrid in VB. Those datagrids are created at the design time. You
just set the datasource later. Of course, you could see the datasource
property.

My question is for the datagrid which is created at run time. Then
there is no datasouce property in VB.net but there is this property in
C#. I do thing sometime C# has more power than VB.NET.

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

thanks,
Donna
 
D

Donna

Elton,
Here is the code. The url you indicate is not dynamically created
datagrid in VB. Those datagrids are created at the design time. You
just set the datasource later. Of course, you could see the datasource
property.

My question is for the datagrid which is created at run time. Then
there is no datasouce property in VB.net but there is this property in
C#. I do thing sometime C# has more power than VB.NET.

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

thanks,
Donna
 
E

Elton Wang

Hi Donna,

I don't have VS at hand today. I will test it tomorrow. If
it's true, it apparently is a bug. BTW, which VS.NET
version are you using?

It's true in some part C# is more powerful but in some
other part VB.NET is more powerful and convenient.

Elton
 
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.

Most of situation VB.net are very similiar to C#. This is actually the
bug in my code somewhere. But thank you very much to help in here.
 

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

Top