2011-02-25 62 views
1

我已经完成了关于如何在网格视图中手动创建排序的研究,但没有一个方法足以让我遵循。我需要更多的一步一步的解决方案,比如我的排序代码应该进入什么事件,我将如何启用标题以允许排序。通常情况下,我只有.net做到这一点,但由于某种原因,这次它不允许它,也许是因为我没有使用数据源。在vb.net中对gridview进行手动排序

这是我的代码,创建一个datable,然后绑定到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 

回答

1

其实我想通了。我必须将排序表达式添加到客户端代码。

要解决我的问题,第一步是将排序表达式添加到客户端代码。

<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 +方向,这将是这两个字符串的工作,因为它会串连一起并用它们作为列名,这将导致程序无法找到之前柱。所以我只是把它改为

transferview.Sort = sortExpression + " " + direction 

它的工作完美。

+0

尼斯。我只是发布了同样的建议。以防万一,如果您好奇这可能会怎样,我会添加一些代码隐藏功能。 – 2011-02-28 15:42:48

+1

您觉得这个问题/答案对其他用户还是有用的吗?如果没有,你可能会考虑删除它......如果另一个答案是你接受的那个答案,那么除非你认为* this *是真正的答案......否则你可以在这里添加它作为评论。 – Benjol 2011-03-01 14:13:17

+0

你的权利我会发布到底是什么 – MyHeadHurts 2011-03-01 14:22:14

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 
+0

我的标题没有boundfields,但我遇到了一个问题。我可以得到的列进行排序,如果我使用dv.Sort = sortExpression,没有方向,但如果我使用dv.Sort = sortExpression&direction然后它使我columnname1而不是列名和asc或desc – MyHeadHurts 2011-02-28 15:49:33

+0

代码可以修改为TemplateFields但它不是必需的,因为您要将其设置在客户端上。你能解释一下更多的列名称发生了什么? – 2011-02-28 15:53:57

+0

另外,当您在GridViewSortDirection属性中创建断点时,是否正确设置了值? – 2011-02-28 15:59:02

0

转换排序顺序

dv.Sort = sortExpression & " " & ConvertSortDirectionToSql(direction) 
相关问题