前台代码
1 <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="496px" AllowSorting="True">
2 <Columns>
3 <asp:BoundColumn HeaderText="id" DataField="id" SortExpression="id"></asp:BoundColumn>
4 <asp:BoundColumn HeaderText="name" DataField="name" SortExpression="name"></asp:BoundColumn>
5 <asp:BoundColumn HeaderText="age" DataField="age" SortExpression="age"></asp:BoundColumn>
6 <asp:BoundColumn HeaderText="sex" DataField="sex" SortExpression="sex"></asp:BoundColumn>
7 </Columns>
8 </asp:DataGrid>
9
2 <Columns>
3 <asp:BoundColumn HeaderText="id" DataField="id" SortExpression="id"></asp:BoundColumn>
4 <asp:BoundColumn HeaderText="name" DataField="name" SortExpression="name"></asp:BoundColumn>
5 <asp:BoundColumn HeaderText="age" DataField="age" SortExpression="age"></asp:BoundColumn>
6 <asp:BoundColumn HeaderText="sex" DataField="sex" SortExpression="sex"></asp:BoundColumn>
7 </Columns>
8 </asp:DataGrid>
9
1 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
2 '在此处放置初始化页的用户代码
3 If Me.IsPostBack = False Then
4 BindDataGrid()
5 End If
6 End Sub
7
8 Private Function GetDatasource() As DataView
9 Dim dt As DataTable = New DataTable
10 dt.Columns.Add("id", GetType(System.Int32))
11 dt.Columns.Add("name", GetType(System.String))
12 dt.Columns.Add("age", GetType(System.Int32))
13 dt.Columns.Add("sex", GetType(System.Boolean))
14
15 Dim dr As DataRow
16 For i As Integer = 0 To 10
17 dr = dt.NewRow
18 dr(0) = i
19 dr(1) = "name" & i
20 dr(2) = 15 + 2 * i
21 dr(3) = i Mod 2
22 dt.Rows.Add(dr)
23 Next
24
25 Dim dv As DataView
26 dv = dt.DefaultView
27
28 If (IsNothing(Me.DataGrid1.Attributes("OrderBy")) = False) Then
29 dv.Sort = Me.DataGrid1.Attributes("OrderBy")
30 End If
31
32 Return dv
33 End Function
34
35 Private Sub BindDataGrid()
36 Me.DataGrid1.DataSource = GetDatasource()
37 Me.DataGrid1.DataBind()
38 End Sub
39
40
41 Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
42 If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
43 If e.Item.Cells(3).Text = "True" Then
44 e.Item.Cells(3).Text = "男"
45 Else
46 e.Item.Cells(3).Text = "女"
47 End If
48 End If
49 End Sub
50
51 Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
52 '找到排序的列,并修改它的排序属性
53 Dim clm As DataGridColumn = Nothing
54 For i As Integer = 0 To Me.DataGrid1.Columns.Count
55 If Me.DataGrid1.Columns(i).SortExpression = e.SortExpression Then
56 clm = Me.DataGrid1.Columns(i)
57 Exit For
58 End If
59 Next
60
61 If e.SortExpression.ToLower().IndexOf("desc") > 0 Then
62 clm.SortExpression = e.SortExpression.ToLower().Replace("desc", "asc")
63 ElseIf e.SortExpression.ToLower().IndexOf("asc") > 0 Then
64 clm.SortExpression = e.SortExpression.ToLower().Replace("asc", "desc")
65 Else
66 clm.SortExpression = e.SortExpression.ToLower() & " desc"
67 End If
68
69 Me.DataGrid1.Attributes("OrderBy") = clm.SortExpression
70
71 BindDataGrid()
72 End Sub
73
2 '在此处放置初始化页的用户代码
3 If Me.IsPostBack = False Then
4 BindDataGrid()
5 End If
6 End Sub
7
8 Private Function GetDatasource() As DataView
9 Dim dt As DataTable = New DataTable
10 dt.Columns.Add("id", GetType(System.Int32))
11 dt.Columns.Add("name", GetType(System.String))
12 dt.Columns.Add("age", GetType(System.Int32))
13 dt.Columns.Add("sex", GetType(System.Boolean))
14
15 Dim dr As DataRow
16 For i As Integer = 0 To 10
17 dr = dt.NewRow
18 dr(0) = i
19 dr(1) = "name" & i
20 dr(2) = 15 + 2 * i
21 dr(3) = i Mod 2
22 dt.Rows.Add(dr)
23 Next
24
25 Dim dv As DataView
26 dv = dt.DefaultView
27
28 If (IsNothing(Me.DataGrid1.Attributes("OrderBy")) = False) Then
29 dv.Sort = Me.DataGrid1.Attributes("OrderBy")
30 End If
31
32 Return dv
33 End Function
34
35 Private Sub BindDataGrid()
36 Me.DataGrid1.DataSource = GetDatasource()
37 Me.DataGrid1.DataBind()
38 End Sub
39
40
41 Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
42 If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
43 If e.Item.Cells(3).Text = "True" Then
44 e.Item.Cells(3).Text = "男"
45 Else
46 e.Item.Cells(3).Text = "女"
47 End If
48 End If
49 End Sub
50
51 Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
52 '找到排序的列,并修改它的排序属性
53 Dim clm As DataGridColumn = Nothing
54 For i As Integer = 0 To Me.DataGrid1.Columns.Count
55 If Me.DataGrid1.Columns(i).SortExpression = e.SortExpression Then
56 clm = Me.DataGrid1.Columns(i)
57 Exit For
58 End If
59 Next
60
61 If e.SortExpression.ToLower().IndexOf("desc") > 0 Then
62 clm.SortExpression = e.SortExpression.ToLower().Replace("desc", "asc")
63 ElseIf e.SortExpression.ToLower().IndexOf("asc") > 0 Then
64 clm.SortExpression = e.SortExpression.ToLower().Replace("asc", "desc")
65 Else
66 clm.SortExpression = e.SortExpression.ToLower() & " desc"
67 End If
68
69 Me.DataGrid1.Attributes("OrderBy") = clm.SortExpression
70
71 BindDataGrid()
72 End Sub
73