Populating DataGrid

V

vbGansta

Hi there!!

Sorry for the simplicity of this one, but I am just getting my feet wet in
this. How do I populate a data control manually from code? In other words
I want to fill it myself not have it bound to a back end database. Is there
another control that would better suit this? Any code examples would be
awesome.

Thanks,

Joe
 
O

ONIL@

Hi vbGansta !

You can use a DataTable like this :

Dim dt As DataTable

Dim dr As DataRow

Dim i As Integer

'create a DataTable

dt = New DataTable

dt.Columns.Add(New DataColumn("Name", GetType(String)))

dt.Columns.Add(New DataColumn("First.", GetType(String)))

dt.Columns.Add(New DataColumn("Last", GetType(String)))

'Make the rows and add data.

For i = 1 To 12

dr = dt.NewRow()

dr(0)= "Hi " & i+1
dr(1) = "Hi" & i+2


dr(2) = "Hi" & i+3

'add each row to the DATATABLE

dt.Rows.Add(dr)

Next

dr = dt.NewRow()

dt.Rows.Add(dr)

DataGrid1.DataSource = new DataView(dt)

DataGrid1.DataBind

I hope it helps
Best Regards
 
K

Ken Cox [Microsoft MVP]

Here's some code that I use, adapted from the VS.NET documentation.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub
Function CreateDataSource() As ICollection
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add _
(New DataColumn("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = True
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top