Easiest way to bind a grid datasource to a datatable

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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()
 
Back
Top