Easiest way to bind a grid datasource to a datatable

G

Guest

Hi,

I want to make sure I got the easiest way on how to bind a grid's datasource to a datatable.

I've made an in-memory datatable

Dim dtTotal As DataTable
Dim drow As DataRow
dtTotal = New DataTable("Total")
' Add two columns
dcol = dtTotal.Columns.Add("Str1", System.Type.GetType("System.String"))
dcol = dtTotal.Columns.Add("Dec1", System.Type.GetType("System.Decimal"))
' Add two rows
drow = dtTotal.NewRow()
drow("Str1") = "The first value"
drow("Dec1") = 1001
dtTotal.Rows.Add(drow)
drow = dtTotal.NewRow()
drow("Str1") = "The second value"
drow("Dec1") = 1002
dtTotal.Rows.Add(drow)

As I understand, you only should need to create a dataview object and bind it to a grid's datasource

Dim dv As DataView
dv = dtTotal.DefaultView
grid.DataSource = dv
grid.DataBind()

Is this the easiest way or the way with least resources on the server?

/Kenneth
 
T

Teemu Keiski

Well,

all I can say is that you don't necessarily need to create the DataView
explicitly for databinding (binding happens to DataView, dt.DefaultView,
automatically if you provide the DataTable as data source)

e.g

grid.DataSource = dtTotal
grid.DataBind()
 

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