Dim strOrderBy As String
Dim dv As New DataView
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Try
strOrderBy = "ID ASC"
ViewState("strOrderBy") = strOrderBy
ViewState("Column") = "ID"
ViewState("Order") = "ASC"
Dim ds2 As New DataSet
'DataGrid2.AutoGenerateColumns = False
DataGrid2.AllowSorting = True
DataGrid2.AllowPaging = True
'Get a DataSet object filled with data
ds2 = Makeds()
'Create ID column & add to DataGrid
Dim col As New BoundColumn
col.HeaderText = "User ID"
col.DataField = "ID"
col.SortExpression = "ID"
DataGrid2.Columns.Add(col)
'Create Name column & add to DataGrid
col = New BoundColumn
col.HeaderText = "User Name"
col.DataField = "Name"
col.SortExpression = "Name"
DataGrid2.Columns.Add(col)
'Create Address column & add to DataGrid
col = New BoundColumn
col.HeaderText = "User Address"
col.DataField = "Address"
col.SortExpression = "Address"
DataGrid2.Columns.Add(col)
'DataGrid data binding
DataGrid2.DataSource = ds2.Tables(0)
DataGrid2.DataBind()
Catch ex As Exception
End Try
End If
dtgCustBind()
End Sub
Function Makeds() As DataSet
Dim myDataTable As DataTable = New DataTable("ParentTable")
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow
Dim myDataset As DataSet
Dim j As Integer
Try
' Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "ID"
myDataColumn.ReadOnly = True
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)
' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "Name"
myDataColumn.AutoIncrement = False
myDataColumn.ReadOnly = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)
' Create third column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "Address"
myDataColumn.AutoIncrement = False
myDataColumn.ReadOnly = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)
' Instantiate the DataSet variable.
myDataset = New DataSet
' Add the new DataTable to the DataSet.
myDataset.Tables.Add(myDataTable)
For j = 1 To 12
myDataRow = myDataTable.NewRow()
myDataRow("id") = j.ToString
myDataRow("Name") = (j + 100).ToString
myDataRow("Address") = j.ToString & " Street"
myDataTable.Rows.Add(myDataRow)
Next
Return myDataset
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Function
Private Sub DataGrid2_SortCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid2.SortCommand
If ViewState("Order") = "ASC" Then
strOrderBy = e.SortExpression & " DESC"
ViewState("Order") = "DESC"
Else
strOrderBy = e.SortExpression & " ASC"
ViewState("Order") = "ASC"
End If
ViewState("strOrderBy") = strOrderBy
ViewState("Column") = e.SortExpression()
dtgCustBind()
End Sub
Private Sub DataGrid2_PageIndexChanged1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid2.PageIndexChanged
Me.DataGrid2.CurrentPageIndex = e.NewPageIndex
dv.Sort = ViewState("Column") & " " & ViewState("Order")
Me.DataGrid2.DataSource = dv
Me.DataGrid2.DataBind()
End Sub
Public Sub dtgCustBind()
Dim ds2 As DataSet
ds2 = Makeds()
Dim dtbl As DataTable = ds2.Tables(0)
dv = New DataView(dtbl)
dv.Sort = ViewState("strOrderBy")
Me.DataGrid2.DataSource = dv
Me.DataGrid2.DataBind()
End Sub
End Class