我想通过单击列标题对GridView进行排序。 到目前为止,我已经声明了SPDataSource

<SharePoint:SPDataSource
                runat="server"
                ID="SPdsInventarGrid"
                DataSourceMode="List"
                UseInternalName="true"
                Scope="Recursive"
                SelectCommand="<View><Query><Where><Or><Neq><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x00210' /><Value Type='Text'>Open</Value></Neq><IsNull><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x0028' /></IsNull></Or></Where></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='ID'/><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x0028'/><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x00210'/><FieldRef </ViewFields></View>">
                <SelectParameters>
                    <asp:Parameter Name="ListName" DefaultValue="Inventar" />
                </SelectParameters>
            </SharePoint:SPDataSource>

然后我根据数据源创建了网格

    <asp:GridView Visible="true" Width="100%"
                ID="gvInventar"
                AutoGenerateColumns="false" runat="server" CellPadding="2" AllowPaging="false" AllowSorting="true" OnSorted="gvInventar_Sorted" OnSorting="gvInventar_Sorting" GridLines="None" DataSourceID="SPdsInventarGrid" OnRowCommand="gvInventar_RowCommand">
                <Columns>
                    <asp:CommandField ButtonType="Image" ShowEditButton="true" EditImageUrl="/_layouts/images/edit.gif" UpdateImageUrl="/_layouts/images/save.gif" CancelImageUrl="/_layouts/images/delete.gif" />

                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Title" >
                        <ItemTemplate>
                            <asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" ></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Product" SortExpression="Product">
                        <ItemTemplate>
                            <asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server" ID="lblProduct" "></asp:Label>
                        </ItemTemplate>

                </Columns>
    </asp:GridView>

现在,我有了这个函数,我想以字符串形式获取当前查询,并在<Query>之前添加<OrderBy> (我的字段),但是我得到一个错误,说明已经声明了数据源(这是正确的,因为我想要在asp中声明)

protected void gvInventar_Sorting(object sender, GridViewSortEventArgs e)
    {
        SPQuery q = new SPQuery();
        q.Query = SPdsInventarGrid.SelectCommand;
        switch (e.SortExpression)
        {
            case "Product":
                if (e.SortDirection == SortDirection.Ascending)
                {
                    gvInventar.DataSource = q.Query;
                    gvInventar.DataBind();
                }
                else
                {
                }
               break;
        }
    }

即使这种方式行不通,我也不认为修改查询是解决方案。 有人可以帮我解决这个问题吗?

===============>>#1 票数:0 已采纳

您无需编写任何自定义代码即可通过单击列标题来对gridview进行排序, SortExpressionTemplateFieldSortExpression属性设置为所需的列名即可,例如:

<Columns>
    <asp:TemplateField HeaderText="ID" SortExpression="ID">
        <ItemTemplate>
            <asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Title" SortExpression="Title">
        <ItemTemplate>
            <asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Product" SortExpression="EBS_x002e_CN_x002e_Inventar_x0028">
        <ItemTemplate>
            <asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server"
                ID="lblProduct" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

现在,您可以对所有3列进行排序:

在此处输入图片说明

  ask by Fulga translate from so

未解决问题?本站智能推荐: