我已经研究过如何在网格视图中手动创建排序,而且没有一个足够让我遵循。我需要更多的逐步解决方案,比如我的排序代码应该进入什么事件,如何启用标题以允许排序。通常情况下,我只有.net这样做,但由于某种原因这次它不允许它,可能是因为我没有使用数据源。
这是我创建数据的代码,然后绑定到gridview。
Function toptable()
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("@yeartoget", DropDownList1.SelectedValue)
cmd.Parameters.AddWithValue("@mode", RadioButtonList1.SelectedValue)
If TextBox1.Text = "" Then
Dim d As Date = Date.Today
TextBox1.Text = d.AddDays(-1)
End If
Dim pyear As Date
Dim pyear1 As Date
pyear = TextBox1.Text
Dim year1 As Int16
year1 = (pyear.Year - DropDownList1.SelectedValue)
If pyear.Year <> Now.Year Then
pyear1 = (pyear.AddYears(-(year1 + 1)))
Else
pyear1 = (pyear.AddYears(-year1))
End If
cmd.Parameters.AddWithValue("@current", TextBox1.Text)
cmd.Parameters.AddWithValue("@search", pyear1)
cmd.CommandText = "asoftour"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
cmd.CommandTimeout = 300
conn.Open()
reader = cmd.ExecuteReader()
mytable.clear()
myTable.Load(reader)
conn.Close()
Dim currentppax As New DataColumn
currentppax = New DataColumn("AVGPPAX", GetType(Double))
currentppax.Expression = "IIf([CurrentPAX] = 0, 0, [CurrentSales] / [CurrentPAX])"
Dim selectedppax As New DataColumn
selectedppax = New DataColumn("AVGpPAX1", GetType(Double))
selectedppax.Expression = "IIf([asofPAX] = 0, 0, [asofSales] / [asofPAX])"
Dim projsales As New DataColumn
projsales = New DataColumn("ProjSales", GetType(Double))
projsales.Expression = "IIF([PercentSales] = 0, [CurrentSales], [CurrentSales] / [PercentSales])"
Dim projpax As New DataColumn
projpax = New DataColumn("ProjPAX", GetType(Double))
' projpax.Expression = "IIF([PercentPAX] = 0, [CurrentPAX], [CurrentPAX] / [percentpax] )"
projpax.Expression = "IIF([PercentPAX] = 0, [CurrentPAX], [CurrentPAX] / [percentpax] )"
Dim remainingsales As New DataColumn
remainingsales = New DataColumn("remainingsales", GetType(Double))
remainingsales.Expression = "ProjSales - currentsales"
Dim remainingpax As New DataColumn
remainingpax = New DataColumn("remainingpax", GetType(Double))
remainingpax.Expression = "Projpax - currentpax"
Dim movementpax As New DataColumn
movementpax = New DataColumn("movementPAX", GetType(Double))
movementpax.Expression = "IIF([ASOFPAX] = 0, 2,(CurrentPAX / [ASOFPAX]))-1"
Dim movementsales As New DataColumn
movementsales = New DataColumn("movementsales", GetType(Double))
movementsales.Expression = "IIF([ASOFsales] = 0, 2,(Currentsales / [ASOFsales]))-1"
myTable.Columns.Add(selectedppax)
myTable.Columns.Add(currentppax)
myTable.Columns.Add(projsales)
myTable.Columns.Add(projpax)
myTable.Columns.Add(remainingsales)
myTable.Columns.Add(remainingpax)
myTable.Columns.Add(movementsales)
myTable.Columns.Add(movementpax)
GridView1.DataSource = myTable
GridView1.DataBind()
Dim row As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
'spanned cell that will span the columns I don't want to give the additional header
Dim compare As TableCell = New TableHeaderCell()
compare.ColumnSpan = 8
row.Cells.Add(compare)
compare.Text = DropDownList1.Text
'spanned cell that will span the columns i want to give the additional header
Dim current As TableCell = New TableHeaderCell()
current.ColumnSpan = 3
current.Text = Year(Now())
row.Cells.Add(current)
Dim Projection As TableCell = New TableHeaderCell()
Projection.ColumnSpan = 4
Projection.Text = "Projections"
row.Cells.Add(Projection)
Dim Movements As TableCell = New TableHeaderCell()
Movements.ColumnSpan = 2
Movements.Text = "Movement"
row.Cells.Add(Movements)
'Add the new row to the gridview as the master header row
'A table is the only Control (index[0]) in a GridView
DirectCast(GridView1.Controls(0), Table).Rows.AddAt(0, row)
这就是我的客户端代码
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" CellSpacing="2" AutoGenerateColumns="False" ShowFooter="True" Font-Size="Smaller" AllowSorting="True" OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField FooterText="Totals:" HeaderText="Regions">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Tour") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Tour") %>'></asp:Label>
</ItemTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="As Of Sales">
<ItemTemplate>
<%#Getsales(Decimal.Parse(Eval("asofsales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#Getsales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="YE Sales">
<ItemTemplate>
<%#Getyesales(Decimal.Parse(Eval("yesales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#Getyesales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PCT Sales">
<ItemTemplate>
<%#GetAvesales(Decimal.Parse(Eval("percentSales").ToString())).ToString("P1")%>
</ItemTemplate>
<FooterTemplate>
<%#GetAvesales1().ToString("P1")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="As Of PAX">
<ItemTemplate>
<%#Getpax(Decimal.Parse(Eval("asofpax").ToString())).ToString("N0")%>
</ItemTemplate>
<FooterTemplate>
<%#Getpax1().ToString("N0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="YE PAX">
<ItemTemplate>
<%#Getyepax(Decimal.Parse(Eval("YEpax").ToString())).ToString("N0")%>
</ItemTemplate>
<FooterTemplate>
<%#Getyepax1().ToString("N0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PCT PAX">
<ItemTemplate>
<%#GetAvepax(Decimal.Parse(Eval("percentpax").ToString())).ToString("P2")%>
</ItemTemplate>
<FooterTemplate>
<%#GetAvepax1().ToString("P1")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="AVG PPAX">
<ItemTemplate>
<%#GetAveppax(Decimal.Parse(Eval("AVGppax1").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetpreviousAveppax1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Current Sales">
<ItemTemplate>
<%#GetCsales(Decimal.Parse(Eval("CurrentSales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetCsales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Current PAX">
<ItemTemplate>
<%#GetCpax(Decimal.Parse(Eval("CurrentPAX").ToString())).ToString("N0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetCpax1().ToString("N0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="AVG PPAX">
<ItemTemplate>
<%#GetAveppax(Decimal.Parse(Eval("AVGppax").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetAveppax1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Proj. Sales">
<ItemTemplate>
<%#GetPsales(Decimal.Parse(Eval("ProjSales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetPsales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Proj. PAX">
<ItemTemplate>
<%#GetPpax(Decimal.Parse(Eval("ProjPax").ToString())).ToString("N0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetPpax1().ToString("N0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Remaining Sales">
<ItemTemplate>
<%#GetRPsales(Decimal.Parse(Eval("remainingsales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetRPsales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Remaining PAX">
<ItemTemplate>
<%#GetRPpax(Decimal.Parse(Eval("RemainingPax").ToString())).ToString("N0")%>
</ItemTemplate>
<FooterTemplate>
<%#GetRPpax1().ToString("N0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PCT. Pax">
<ItemTemplate>
<%#Getmovepax(Decimal.Parse(Eval("movementPAX").ToString())).ToString("P1")%>
</ItemTemplate>
<FooterTemplate>
<%#Getmovepax1().ToString("P1")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PCT. Sales">
<ItemTemplate>
<%#Getmovesales(Decimal.Parse(Eval("movementsales").ToString())).ToString("P1")%>
</ItemTemplate>
<FooterTemplate>
<%#Getmovesales1().ToString("P1")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Theme">
<ItemTemplate>
<asp:Label ID="lbltheme" runat="server" Text="Label"></asp:Label><br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我试过但我的标题上没有链接显示。
Public Property GridViewSortDirection() As SortDirection
Get
If ViewState("sortDirection") Is Nothing Then
ViewState("sortDirection") = SortDirection.Ascending
End If
Return DirectCast(ViewState("sortDirection"), SortDirection)
End Get
Set(ByVal value As SortDirection)
ViewState("sortDirection") = value
End Set
End Property
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
Dim sortExpression As String = e.SortExpression
If GridViewSortDirection = SortDirection.Ascending Then
GridViewSortDirection = SortDirection.Descending
SortGridView(sortExpression, SortDirection.Descending)
Else
GridViewSortDirection = SortDirection.Ascending
SortGridView(sortExpression, SortDirection.Ascending)
End If
End Sub
Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
' You can cache the DataTable for improving performance
Dim dv As New DataView(myTable)
dv.Sort = sortExpression & direction
GridView1.DataSource = dv
GridView1.DataBind()
End Sub
答案 0 :(得分:1)
实际上,我想通了。我不得不将sort表达式添加到客户端代码中。
要解决我的问题,第一步是将排序表达式添加到客户端代码中。
<asp:TemplateField HeaderText="As Of Sales" SortExpression= "asofsales">
这解决了我的问题,允许排序,但它只能按一个方向升序排序。我有正确的排序代码问题是我必须添加几个东西。
Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
If direction = "1" Then
direction = "ASC"
Else
direction = "DESC"
End If
' You can cache the DataTable for improving performance
transferview.Sort = sortExpression + " " + direction
GridView1.DataSource = transferview
GridView1.DataBind()
End Sub
End Class
在我使用之前 transferview.Sort = sortExpression + direction,因为它会连接两个字符串并且将它们用作列名,这将导致程序找不到列,因此无法工作。所以我只是将其改为
transferview.Sort = sortExpression + " " + direction
它完美无缺。
答案 1 :(得分:1)
您需要设置SortExpression。
您可以使用以下内容在代码后面执行此操作:
Private Sub setSortExpression()
Try
Dim field As DataControlField
For Each field In gridView.Columns
If TypeOf field Is BoundField Then
Dim dbf As BoundFielddbf = DirectCast(field, BoundField)
field.SortExpression = dbf.DataField
End If
Next
Catch ex As Exception
End Try
End Sub
答案 2 :(得分:0)
转换排序顺序
dv.Sort = sortExpression & " " & ConvertSortDirectionToSql(direction)