datagrid as reusable datasource?

  • Thread starter Thread starter Richard K Bethell
  • Start date Start date
R

Richard K Bethell

I have a page that does some complex calculations in order to serve up a
datatable that is then bound to a datagrid. I want to add sort to this
datagrid.

Now I know that to prevent a recalc, I could throw the DataTable in the
viewstate and rebind at every hit. However, I am wondering if I have this
available as an option - can I assign a DataTable the value of the
..DataSource property of the datagrid? It would lighten the burden on the
browsers some if I could...

R.
 
Hi Richard,

If I didn't misunderstand you, you want to populate a datatable from the
datasource bound to a datagrid. If it is the case, you can get the binding
datatable through the DataSource property of a datagrid. Suppose that a
datagrid was bound to a dataview. For example,

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
' Load this data only once.
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)))

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)

dt.Rows.Add(dr)
Next i

Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource

If you want to get the datatable bound to the datagrid, you can do as
follows:

Dim dt As DataTable
Dim dv As DataView
dv = DataGrid1.DataSource
dt = dv.Table

Response.Write(dt.Columns.Count)

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top