Dynamic DataSet In A DataGrid

  • Thread starter Thread starter Arpan
  • Start date Start date
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
 
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
 
Back
Top