1

我想通过单击列的标题对我的 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>

现在,我有这个函数,我想将当前查询作为字符串获取并在之前添加<OrderBy>(我的字段),<Query>但是我收到一个错误,说 DataSource 已经被声明(这是真的,因为我想在 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;
        }
    }

即使它不以这种方式工作,我也不认为修改 Query 是解决方案。谁能帮我解决这个问题?

4

1 回答 1

0

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

<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 列进行排序:

在此处输入图像描述

于 2013-04-16T07:29:13.923 回答