按正确的列(即 TemplateField)对 gridView 进行排序

发布于 2024-12-08 13:20:01 字数 1362 浏览 1 评论 0原文

我在 gridView 中排序列时遇到问题。我只想选择列中名称等于“Finished”的行。

我的 gridView 由几个 BoundField 和 TamplateFields 组成。当我想按正确的 BoundField 排序时,我将正确的字符串放入属性 FilterExpression 中。例如:

GridView 中的字段:

所以我设置 FilterExpression = "[identifier] LIKE '%" + txtCaseNumber.Text + "%'"

其中 identifier 是 BoundField 中的 DataField,在这种情况下一切正常。 但是,当我想从 TempateField 中名称等于 Fininish 的列中选择正确的行时,我不知道应该如何做。 TemplateField 的示例如下所示:

<asp:TemplateField HeaderText="Status" SortExpression="Status">
                    <ItemTemplate>
                        <asp:Label ID="lblStatus" runat="server" Text='<%# Utils.ConvertToProcessStatus((int)Eval("status"))%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

如果有人知道我该怎么做?

感谢您的帮助。


没关系,但我忘了提到我有自己的用户控件(ucCaseFilter),它由我从中选择状态的 DropDownList 组成。例如:失败、进行中、完成...在此控件内,我创建公共属性过滤器,我想分配字符串值,该值将选择正确的列并对它们进行排序。

Filter = "[???] = '" + ddlCaseStatus.SelectedValue + "'"; 

然后在 gridView 中使用它:

FilterExpression = ucCaseFilter.Filter;

我不知道如何获取列名称或其他方法来让我按“状态”列排序。

如果我不明白之前的回答并且它指的是正确的解决方案

I have got problem with sorting column in gridView. I want to select only those rows in column which name equal for example "Finished"

My gridView consists of few BoundField and TamplateFields. When I want to sort by proper BoundField I put proper string to property FilterExpression. For example:

Field in GridView:
<asp:BoundField DataField="identifier" HeaderText="Case number" SortExpression="identifier" />

so I set
FilterExpression = "[identifier] LIKE '%" + txtCaseNumber.Text + "%'"

where identifier is DataField in BoundField and In this case everything is ok.
But when I want to select proper rows from column wihich name equal Fininish from TempateField I don't know how I should do it.
Exaple of TemplateField looks like this:

<asp:TemplateField HeaderText="Status" SortExpression="Status">
                    <ItemTemplate>
                        <asp:Label ID="lblStatus" runat="server" Text='<%# Utils.ConvertToProcessStatus((int)Eval("status"))%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

If anyone know how I can do it??

Thanks for help.


It's ok but I forgot mention that I have my own user contron (ucCaseFilter) which consists of DropDownList from which I choose status. For example: Failed, InProgress, Finish ... Inside this control I create public property Filter and I want to assign string value which will be select proper column and sort them .

Filter = "[???] = '" + ddlCaseStatus.SelectedValue + "'"; 

Then in gridView use it:

FilterExpression = ucCaseFilter.Filter;

I don't know how get column name or other approach which afford me sort by "Status" column.

If I didn't understand previous answerd and it refer to correct soluti

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2024-12-15 13:20:01

更改您的 以添加带有 LinkBut​​ton 标记,如下所示

<asp:TemplateField SortExpression="Status">
    <HeaderTemplate>
        <asp:LinkButton ID="lnkSort" runat="server" Text="Status" CommandName="Sort" CommandArgument="Status" />                
    </HeaderTemplate>
</asp:TemplateField>

: >grd_RowCommand 事件处理“排序”命令并使用 CommandArgument 构建过滤字符串

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Sort"))
    {
        FilterExpression = e.CommandArgument.ToString() + " LIKE '%" + txtCaseNumber.Text + "%'";
        BindGridView();
    }   
}

Change your <asp:TempleteField /> to add <HeaderTemplate /> markup with a LinkButton like this:

<asp:TemplateField SortExpression="Status">
    <HeaderTemplate>
        <asp:LinkButton ID="lnkSort" runat="server" Text="Status" CommandName="Sort" CommandArgument="Status" />                
    </HeaderTemplate>
</asp:TemplateField>

and in grd_RowCommand event handle "Sort" command and use the CommandArgument to build your filter string

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Sort"))
    {
        FilterExpression = e.CommandArgument.ToString() + " LIKE '%" + txtCaseNumber.Text + "%'";
        BindGridView();
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文