在gridview中对Expression多头进行排序

时间:2013-04-04 21:04:02

标签: asp.net vb.net gridview

我的gridview中有2个标题。我在RowCreated事件后面的代码中插入第二个标题,该事件将插入<asp:BoundField>行之上。我已为每个SortExpression添加了BoundField属性。当我运行应用程序时SorExpression超链接正在每个BoundField上创建而不是在插入的标题上(它在逻辑上是正确的)。但是我想在插入的标题行上进行sortexpression。我怎么能这样做?

下面我正在解释我做的步骤

ASPX

<asp:GridView ID="gvInitiavtives" runat="server" Width="100%" CssClass="Grid" 
       RowStyle-Width="30px" AutoGenerateColumns="false" HeaderStyle-CssClass="GridHeader" RowStyle-CssClass="GridItem" AlternatingRowStyle-CssClass="GridAltItem"  DataKeyNames="InitiativeIdx"  >
      <Columns>
      <asp:BoundField DataField="BusinessUnit" HeaderText="" HeaderStyle-Wrap="false" HeaderStyle-HorizontalAlign="NotSet" SortExpression='BusinessUnit' />
      <asp:BoundField DataField="IFunction" HeaderText="" HeaderStyle-Wrap="false" HeaderStyle-HorizontalAlign="NotSet" SortExpression='IFunction'/>
      <asp:BoundField DataField="SubFunction" HeaderText="" HeaderStyle-Wrap="false" HeaderStyle-HorizontalAlign="NotSet" SortExpression='SubFunction' />         
    .....

背后的代码

  Private Sub gvInitiavtives_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvInitiavtives.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim HeaderGrid As GridView = DirectCast(sender, GridView)
            Dim HeaderGridRow As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)

            Dim HeaderCell As New TableHeaderCell()
            HeaderCell.Text = "BU"
            HeaderGridRow.Cells.Add(HeaderCell)

            HeaderCell = New TableHeaderCell()
            HeaderCell.Text = "Function"

            HeaderGridRow.Cells.Add(HeaderCell)

            HeaderCell = New TableHeaderCell()
            HeaderCell.Text = "Sub - Function"
            HeaderGridRow.Cells.Add(HeaderCell)
    ....
    .... 
       gvInitiavtives.Controls(0).Controls.AddAt(0, HeaderGridRow)

    End If
End Sub

浏览器

enter image description here

HTML

   <table class="Grid" cellspacing="0" rules="all" border="1" id="Body_gvInitiavtives" style="width:100%;border-collapse:collapse;">
            <tr class="GridHeader">
                <th>BU</th><th>Function</th><th>Sub - Function</th><th>Initiative Name</th><th>Location</th><th>Cost Center</th><th colspan="2">Estimated Cost Savings</th><th colspan="2">Estimated Personnel Savings</th>
            </tr>
<tr class="GridHeader">
            <th scope="col" style="white-space:nowrap;"><a href="javascript:__doPostBack(&#39;ctl00$Body$gvInitiavtives&#39;,&#39;Sort$BusinessUnit&#39;)"></a></th><th scope="col" style="white-space:nowrap;"><a href="javascript:__doPostBack(&#39;ctl00$Body$gvInitiavtives&#39;,&#39;Sort$IFunction&#39;)"></a></th><th scope="col" style="white-space:nowrap;"><a href="javascript:__doPostBack(&#39;ctl00$Body$gvInitiavtives&#39;,&#39;Sort$SubFunction&#39;)"></a></th><th scope="col">&nbsp;</th><th scope="col" style="white-space:nowrap;">&nbsp;</th><th scope="col" style="white-space:nowrap;">&nbsp;</th><th scope="col" style="white-space:nowrap;">Low</th><th scope="col" style="white-space:nowrap;">High</th><th scope="col" style="white-space:nowrap;">Low</th><th scope="col" style="white-space:nowrap;">High</th>
    </tr>
    <tr class="GridItem" style="width:30px;">
   <td>UNKNOWN</td><td>UNKNOWN</td><td>UNKNOWN</td><td>

帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以在每个列的标题行上添加排序表达式,在不需要排序的列中,然后将排序表达式设为空,如

SortExpression =“” - 您不想在哪一列中应用排序