Dynamic DataSet In A DataGrid

A

Arpan

The following code snippet creates a DataSet on the fly which is then passed to a DataView so that the DataSet can finally be viewed in a DataGrid control:

'Create the empty DataSet
Dim objDS As New DataSet("MyDataSet")=CreateDataSet()

'Create the new table & add columns
Dim dTable As New DataTable("Users")
dTable.Columns.Add("ID",System.Type.GetType("System.Int32"))
dTable.Columns.Add("FirstName",System.Type.GetType("System.String"))
dTable.Columns.Add("LastName",System.Type.GetType("System.String"))
dTable.Columns("ID").AutoIncrement=True

'Add the new table to the DataSet's TableCollection
objDS.Tables.Add(dTable)

'Define the Primary Key
Dim dColumn As DataColumn={objDS.Tables("Users").Columns("ID")}
objDS.Tables("Users").PrimaryKey=dColumn

'Add a row to this table
Dim dRow As DataRow=dTable.NewRow()
dRow(1)="Celine"
dRow(2)="Dion"
dTable.Rows.Add(dRow)

Dim objDV As New DataView
objDV.Table=objDS.Tables("Users")
myDataGrid.DataSource=objDV
myDataGrid.DataBind()

Function CreateDataSet
Dim objDS As DataSet=New DataSet("Users")
objDS.Tables.Add(dTable)
Return objDS
End Function

<asp:DataGrid id="myDataGrid" runat=server/>

As such the above code doesn't generate any error but the DataSet isn't getting displayed in the DataGrid! Where am I going wrong?

Please note that I am a newbie in ASP.NET & am working on Windows 2000 Professional.

Thanks,

Arpan
 
?

=?iso-8859-1?Q?Yunus_Emre_ALP=D6ZEN?=

Your code is not tidy. When you create dataset. Most of this problems are caused by process order. Also you don't need to create a new view. Simply you can write so;

myDataGrid.DataSource=objDS
myDataGrid.DataMember="Users"
myDataGrid.DataBind()


Setting DataMember property allows you to choose datatable from a dataset
--
Thanks,
Yunus Emre ALPÖZEN



The following code snippet creates a DataSet on the fly which is then passed to a DataView so that the DataSet can finally be viewed in a DataGrid control:

'Create the empty DataSet
Dim objDS As New DataSet("MyDataSet")=CreateDataSet()

'Create the new table & add columns
Dim dTable As New DataTable("Users")
dTable.Columns.Add("ID",System.Type.GetType("System.Int32"))
dTable.Columns.Add("FirstName",System.Type.GetType("System.String"))
dTable.Columns.Add("LastName",System.Type.GetType("System.String"))
dTable.Columns("ID").AutoIncrement=True

'Add the new table to the DataSet's TableCollection
objDS.Tables.Add(dTable)

'Define the Primary Key
Dim dColumn As DataColumn={objDS.Tables("Users").Columns("ID")}
objDS.Tables("Users").PrimaryKey=dColumn

'Add a row to this table
Dim dRow As DataRow=dTable.NewRow()
dRow(1)="Celine"
dRow(2)="Dion"
dTable.Rows.Add(dRow)

Dim objDV As New DataView
objDV.Table=objDS.Tables("Users")
myDataGrid.DataSource=objDV
myDataGrid.DataBind()

Function CreateDataSet
Dim objDS As DataSet=New DataSet("Users")
objDS.Tables.Add(dTable)
Return objDS
End Function

<asp:DataGrid id="myDataGrid" runat=server/>

As such the above code doesn't generate any error but the DataSet isn't getting displayed in the DataGrid! Where am I going wrong?

Please note that I am a newbie in ASP.NET & am working on Windows 2000 Professional.

Thanks,

Arpan
 

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,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top