質問編集履歴
5
並べ替えが機能したコードを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,6 +12,9 @@
|
|
12
12
|
となるようにするにはどのようにすればよいでしょうか?
|
13
13
|
|
14
14
|
追記です。
|
15
|
+
使用した並べ替え対象のデータ
|
16
|
+

|
17
|
+
|
15
18
|
test.aspx
|
16
19
|
```aspx
|
17
20
|
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sorttest.aspx.vb" Inherits="test.sorttest" %>
|
4
pageloadの記載漏れのため修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,7 +14,7 @@
|
|
14
14
|
追記です。
|
15
15
|
test.aspx
|
16
16
|
```aspx
|
17
|
-
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sorttest.aspx.vb" Inherits="
|
17
|
+
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sorttest.aspx.vb" Inherits="test.sorttest" %>
|
18
18
|
|
19
19
|
<!DOCTYPE html>
|
20
20
|
|
3
pageloadの記載漏れのため修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,50 +13,114 @@
|
|
13
13
|
|
14
14
|
追記です。
|
15
15
|
test.aspx
|
16
|
-
```
|
16
|
+
```aspx
|
17
|
-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="フィールドa" AllowSorting="True" AllowCustomPaging="True" OnSorting="OnSorted" >
|
18
|
-
<Columns>
|
19
|
-
<
|
17
|
+
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sorttest.aspx.vb" Inherits="pythontest.sorttest" %>
|
20
|
-
</asp:ButtonField>
|
21
|
-
<asp:BoundField DataField="フィールドa" HeaderText="フィールドa" SortExpression="フィールドa" />
|
22
|
-
<asp:ImageField DataImageUrlField="フィールドa" DataImageUrlFormatString="./test/images/{0}.png" HeaderText="イメージ">
|
23
|
-
</asp:ImageField>
|
24
|
-
<asp:BoundField DataField="フィールドb" HeaderText="フィールドb" SortExpression="フィールドb" />
|
25
|
-
<asp:BoundField DataField="フィールドc" HeaderText="フィールドc" SortExpression="フィールドc" />
|
26
|
-
<asp:BoundField DataField="フィールドd" HeaderText="フィールドd" SortExpression="フィールドd" />
|
27
|
-
</Columns>
|
28
|
-
<SortedAscendingHeaderStyle CssClass="sortedAscHeader" BackColor="#FFCCFF" />
|
29
|
-
<SortedDescendingHeaderStyle CssClass="sortedDscHeader" BackColor="#99CCFF" />
|
30
|
-
</asp:GridView>
|
31
18
|
|
32
|
-
<
|
19
|
+
<!DOCTYPE html>
|
33
20
|
|
21
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
22
|
+
<head runat="server">
|
23
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
24
|
+
<title></title>
|
25
|
+
<style type="text/css">
|
26
|
+
th.sortedAscHeader,
|
27
|
+
th.sortedDscHeader {
|
28
|
+
background-repeat: no-repeat;
|
29
|
+
background-position: right;
|
30
|
+
padding-right:18px;
|
31
|
+
}
|
34
32
|
|
33
|
+
th.sortedAscHeader {
|
34
|
+
background-image: url(Images/arrow-up.png);
|
35
|
+
}
|
36
|
+
|
37
|
+
th.sortedDscHeader {
|
38
|
+
background-image: url(Images/arrow-down.png);
|
39
|
+
}
|
40
|
+
</style>
|
41
|
+
|
42
|
+
</head>
|
43
|
+
<body>
|
44
|
+
<form id="form1" runat="server">
|
45
|
+
<div>
|
46
|
+
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
|
47
|
+
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSorting="onsorting" PageSize="60" DataKeyNames="フィールドA">
|
48
|
+
<Columns>
|
49
|
+
<asp:CommandField ShowSelectButton="True" />
|
50
|
+
<asp:BoundField DataField="フィールドA" HeaderText="フィールドA" SortExpression="フィールドA" />
|
51
|
+
<asp:BoundField DataField="フィールドB" HeaderText="フィールドB" SortExpression="フィールドB" />
|
52
|
+
<asp:BoundField DataField="フィールドC" HeaderText="フィールドC" SortExpression="フィールドC" />
|
53
|
+
</Columns>
|
54
|
+
<SortedAscendingHeaderStyle CssClass="sortedAscHeader" BackColor="#FFCCFF" />
|
55
|
+
|
56
|
+
<SortedDescendingHeaderStyle CssClass="sortedDscHeader" BackColor="#99CCFF" />
|
57
|
+
|
58
|
+
|
59
|
+
</asp:GridView>
|
60
|
+
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>" ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" SelectCommand="SELECT * FROM [sorttest]"></asp:SqlDataSource>
|
61
|
+
|
62
|
+
<asp:HiddenField ID="hiddenselectcommand" runat="server" />
|
63
|
+
<br />
|
64
|
+
<br />
|
65
|
+
</div>
|
66
|
+
</form>
|
67
|
+
</body>
|
68
|
+
</html>
|
69
|
+
|
70
|
+
|
35
71
|
```
|
36
72
|
test.aspx.vb
|
37
73
|
```VB
|
38
74
|
|
75
|
+
Public Class sorttest
|
39
|
-
|
76
|
+
Inherits System.Web.UI.Page
|
40
77
|
|
78
|
+
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
|
79
|
+
SqlDataSource1.SelectCommand = "Select * FROM [sorttest]" & hiddenselectcommand.Value
|
80
|
+
|
81
|
+
End Sub
|
82
|
+
|
83
|
+
|
84
|
+
Protected Sub OnSorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting
|
85
|
+
|
41
86
|
Dim field As String = e.SortExpression
|
42
87
|
Dim direction As String
|
43
88
|
Dim selectcommandstr As String = SqlDataSource1.SelectCommand
|
44
89
|
|
90
|
+
|
45
91
|
If selectcommandstr.IndexOf("ASC") <> -1 Then
|
46
92
|
direction = "DESC"
|
47
93
|
Else
|
48
94
|
direction = "ASC"
|
49
95
|
End If
|
50
|
-
|
51
96
|
If selectcommandstr.IndexOf("order") <> -1 Then selectcommandstr = selectcommandstr.Remove(selectcommandstr.IndexOf("order"))
|
52
97
|
selectcommandstr = selectcommandstr & " order by IsNumeric([" & field & "]), Val([" & field & "]) " & direction & ", [" & field & "] " & direction & ""
|
53
98
|
|
99
|
+
|
54
100
|
SqlDataSource1.SelectCommand = selectcommandstr
|
55
101
|
|
102
|
+
If selectcommandstr.IndexOf("where") <> -1 Then
|
103
|
+
selectcommandstr = selectcommandstr.Remove(0, selectcommandstr.IndexOf("where"))
|
104
|
+
Else
|
105
|
+
|
106
|
+
If selectcommandstr.IndexOf("order") <> -1 Then
|
107
|
+
selectcommandstr = selectcommandstr.Remove(0, selectcommandstr.IndexOf("order"))
|
108
|
+
Else
|
109
|
+
selectcommandstr = ""
|
110
|
+
End If
|
111
|
+
End If
|
112
|
+
|
113
|
+
hiddenselectcommand.Value = selectcommandstr
|
114
|
+
|
56
115
|
e.Cancel = True
|
57
116
|
|
58
|
-
End Sub
|
117
|
+
End Sub
|
59
118
|
|
119
|
+
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
|
120
|
+
TextBox1.Text = GridView1.SelectedValue.ToString
|
121
|
+
End Sub
|
122
|
+
End Class
|
123
|
+
|
60
124
|
```
|
61
125
|
|
62
126
|
上記とすることで、gridviewのヘッダーをクリックするたびに昇順降順を切り替えて
|
2
asp:SqlDataSource が記載漏れだったため、修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -29,6 +29,9 @@
|
|
29
29
|
<SortedDescendingHeaderStyle CssClass="sortedDscHeader" BackColor="#99CCFF" />
|
30
30
|
</asp:GridView>
|
31
31
|
|
32
|
+
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [datasheet] WHERE ([フィールドa] <> '') ORDER BY [フィールドa] ASC"/>
|
33
|
+
|
34
|
+
|
32
35
|
```
|
33
36
|
test.aspx.vb
|
34
37
|
```VB
|
1
並べ替えが機能したコードを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,4 +9,61 @@
|
|
9
9
|
これを
|
10
10
|
1→2→10→20→30→あいうえお
|
11
11
|
|
12
|
-
となるようにするにはどのようにすればよいでしょうか?
|
12
|
+
となるようにするにはどのようにすればよいでしょうか?
|
13
|
+
|
14
|
+
追記です。
|
15
|
+
test.aspx
|
16
|
+
```HTML
|
17
|
+
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="フィールドa" AllowSorting="True" AllowCustomPaging="True" OnSorting="OnSorted" >
|
18
|
+
<Columns>
|
19
|
+
<asp:ButtonField ButtonType="Button" CommandName="select" HeaderText="選択" Text="選択">
|
20
|
+
</asp:ButtonField>
|
21
|
+
<asp:BoundField DataField="フィールドa" HeaderText="フィールドa" SortExpression="フィールドa" />
|
22
|
+
<asp:ImageField DataImageUrlField="フィールドa" DataImageUrlFormatString="./test/images/{0}.png" HeaderText="イメージ">
|
23
|
+
</asp:ImageField>
|
24
|
+
<asp:BoundField DataField="フィールドb" HeaderText="フィールドb" SortExpression="フィールドb" />
|
25
|
+
<asp:BoundField DataField="フィールドc" HeaderText="フィールドc" SortExpression="フィールドc" />
|
26
|
+
<asp:BoundField DataField="フィールドd" HeaderText="フィールドd" SortExpression="フィールドd" />
|
27
|
+
</Columns>
|
28
|
+
<SortedAscendingHeaderStyle CssClass="sortedAscHeader" BackColor="#FFCCFF" />
|
29
|
+
<SortedDescendingHeaderStyle CssClass="sortedDscHeader" BackColor="#99CCFF" />
|
30
|
+
</asp:GridView>
|
31
|
+
|
32
|
+
```
|
33
|
+
test.aspx.vb
|
34
|
+
```VB
|
35
|
+
|
36
|
+
Protected Sub OnSorted(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting
|
37
|
+
|
38
|
+
Dim field As String = e.SortExpression
|
39
|
+
Dim direction As String
|
40
|
+
Dim selectcommandstr As String = SqlDataSource1.SelectCommand
|
41
|
+
|
42
|
+
If selectcommandstr.IndexOf("ASC") <> -1 Then
|
43
|
+
direction = "DESC"
|
44
|
+
Else
|
45
|
+
direction = "ASC"
|
46
|
+
End If
|
47
|
+
|
48
|
+
If selectcommandstr.IndexOf("order") <> -1 Then selectcommandstr = selectcommandstr.Remove(selectcommandstr.IndexOf("order"))
|
49
|
+
selectcommandstr = selectcommandstr & " order by IsNumeric([" & field & "]), Val([" & field & "]) " & direction & ", [" & field & "] " & direction & ""
|
50
|
+
|
51
|
+
SqlDataSource1.SelectCommand = selectcommandstr
|
52
|
+
|
53
|
+
e.Cancel = True
|
54
|
+
|
55
|
+
End Sub
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
上記とすることで、gridviewのヘッダーをクリックするたびに昇順降順を切り替えて
|
60
|
+
昇順
|
61
|
+
1→2→10→20→30→あい→うえお
|
62
|
+
降順
|
63
|
+
30→20→10→2→1→うえお→あい
|
64
|
+
と並び替えられるようになりました、が、
|
65
|
+
e.cancelとしていることでgridviewの機能としては並び替わったことにならないため、SortedAscendingHeaderStyleが反映されなくなってしまいました。
|
66
|
+
できれば並び替わっている列とその向きがわかるようにしたいのですが、gridviewのsortイベントを介さずにgiridviewに現在並び替えられているフィールドとその向きを設定するなどして、すでに並び替わったことに出来ないでしょうか。
|
67
|
+
gridview.SortDirectionを書き換えようとしてみましたが、readonlyのため無理なようでした。
|
68
|
+
|
69
|
+
追加ですみませんが、ご存知の方がいらっしゃいましたら教えて下さい。
|