Inserting a dynamic column into a DataGrid and sorting by it

We recently had a customer that wanted to sort a dynamic column in a DataGrid and so I thought it may be something that others would like to be able to do.

You get your data for your dynamic column and then do something like below to insert it into an existing DataTable in a DataSet:

Dim dc as new DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.Caption = "DynColumn"
dc.ColumnName = "DynColumn"
 
vServersSet.Tables(0).Columns.Add(dc)
 
For I = 0 To vServersSet.Tables(0).Rows.Count - 1
    Dim get_value As String
    get_value = GetData(I)
    vServersSet.Tables(0).Rows(I).Item("DynColumn") = get_value
Next

Now apply sorting:

vServersSet.Tables(0).DefaultView.Sort = "DynColumn Desc"

This way we can sort on our dynamic column in our grid.  To point the Grid to the table, just using something like:

DataGrid1.DataSource = vServersSet.Tables(0)
DataGrid1.DataBind()
Published 06 August 08 06:00 by Tom
Filed under: , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# a-foton » Inserting a dynamic column into a DataGrid and sorting by it said on August 6, 2008 6:04 AM:

PingBack from http://blog.a-foton.ru/2008/08/inserting-a-dynamic-column-into-a-datagrid-and-sorting-by-it/

# natural genius said on August 7, 2008 7:41 AM:

its a good stuff... makes me rem defaultview

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Page view tracker