asp net grid view按文本排序绑定字段

时间:2017-11-28 08:58:15

标签: c# asp.net sorting

我正在使用带有BoundFields和TemplatedFields的asp gridview。

<asp:GridView ID="RepairGrid" runat="server" CssClass="subitem w100p" 
   AutoGenerateColumns="False"
              OnRowCommand="RepairGrid_SelectedIndexChanged" OnRowDataBound="RepairGrid_RowDataBound" 
              AllowSorting="True" OnSorting="SortGrid">
    <RowStyle CssClass="itemRowStyle"/>
    <AlternatingRowStyle CssClass="alternatingItemRowStyle"/>
    <HeaderStyle CssClass="headerRowStyle"/>
    <Columns>
        <asp:TemplateField HeaderText="PDF" ItemStyle-CssClass="itemFieldStyle"
                           meta:resourcekey="ColumnPDF" HeaderStyle-CssClass="headerFieldStyle">
            <ItemTemplate>
                <asp:ImageButton ID="btnExportPDF" runat="server" CommandArgument='<%# Eval("RepairId") %>'
                                 CommandName="ExportPDF" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_pdf.png" %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="RepairId" SortExpression="RepairId" ItemStyle-CssClass="itemFieldStyle"
                           meta:resourcekey="ColumnRepairId" HeaderStyle-CssClass="headerFieldStyle" >
            <ItemTemplate>
                <asp:LinkButton ID="lnkRepairId" CommandArgument='<%# Eval("RepairId") %>' CommandName="Redirect"
                                runat="server" Text='<%# Eval("RepairId") %>'/>
                <asp:Label ID="lblRepairId" runat="server" Text='<%# Eval("RepairId") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ProblemCategory" SortExpression="InternalDescription" ItemStyle-CssClass="itemFieldStyle"
                           meta:resourcekey="repairItemText" HeaderStyle-CssClass="headerFieldStyle">
            <ItemTemplate>
                <asp:LinkButton ID="lnkProblemCategory" CommandArgument='<%# Eval("RepairId") %>' CommandName="Redirect"
                                runat="server"/>
                <asp:Label ID="lblProblemCategory" runat="server"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="CreateDate" HeaderText="Status" SortExpression="CreateDate" ItemStyle-CssClass="itemFieldStyle"
                        meta:resourcekey="ColumnCreateDate" HeaderStyle-CssClass="headerFieldStyle"/>
        <asp:BoundField DataField="CreateUser" HeaderText="Status" SortExpression="CreateUser" ItemStyle-CssClass="itemFieldStyle"
                        meta:resourcekey="ColumnCreateUser" HeaderStyle-CssClass="headerFieldStyle"/>
        <asp:BoundField DataField="ChangeDate" HeaderText="Status" SortExpression="ChangeDate" ItemStyle-CssClass="itemFieldStyle"
                        meta:resourcekey="ColumnChangeDate" HeaderStyle-CssClass="headerFieldStyle"/>
        <asp:BoundField DataField="ChangeUser" HeaderText="Status" SortExpression="ChangeUser" ItemStyle-CssClass="itemFieldStyle"
                        meta:resourcekey="ColumnChangeUser" HeaderStyle-CssClass="headerFieldStyle"/>
    </Columns>
</asp:GridView>

我已经设法正确地对Boundfields和RepairId模板化字段进行排序,但由于我的TemplatedField ProblemCategory在DataSource中没有它的文本并且将像这样填充

 foreach (RepairDataSet.tms_v_Repair_RepairItemRow row in tblCategories)
 {
    RepairDataSet.tms_RepairItemDataTable repItemTbl = new RepairDataSet.tms_RepairItemDataTable();
    repItemTbl = repairBo.GetRepairItem(row.RepairItemId);
    if (repItemTbl.Count > 0)
    {
       lnkProblemCategory.Text += "<br />" + repItemTbl[0].TextKey;
    }
    else
    {
       lnkProblemCategory.Text += "<br />";
    }
}

在Grid_SelectedIndexChanged中,我已经为sort事件args做了特殊情况,我应该定义DataSource。

是否有可能按文本而不是DB中的值来对TemplatedField进行排序?

1 个答案:

答案 0 :(得分:0)

解决方案一:Sort Menually

<asp:TemplateField>
            <HeaderTemplate>
                <asp:LinkButton ID="LinkButtonEmpName" runat="server" Text="Employee Name" CommandName="Sort" CommandArgument="Employees">
                </asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelEmployee" runat="server" Text='<%# Bind("Employees") %>'/>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="TextBoxEmployee" runat="server"/>
            </FooterTemplate>
</asp:TemplateField >

解决方案2 Sort By Columnd

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

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

解决方案3 Sort with custom templates

<asp:TemplateField HeaderText="Category" SortExpression="Category">
   <ItemTemplate>A value</ItemTemplate>
</asp:TemplateField>