Home All Groups Group Topic Archive Search About

dynamically create datagrid and set its datasource in VB.NET

Author
16 Mar 2005 2:59 AM
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

Author
17 Mar 2005 1:39 AM
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.


Donna wrote:
Show quoteHide quote
> 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
Author
17 Mar 2005 2:59 PM
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.
>
>
>Donna wrote:
>> I try to dynamically add datagrid to my web form in
VB.NET.  But when
Show quoteHide quote
>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
>
>.
>