c# - Gridview 排序不起作用

标签 c# asp.net sorting gridview

我尝试使用优化的分页实现排序,但是当我尝试排序时,没有任何反应。排序 header 的设计发生了变化,仅此而已。

GridView :

<div align="center">

     <asp:ObjectDataSource ID="odsSupplier" runat="server" SelectMethod="GetSuppliers"
        TypeName="Supplier" EnablePaging="True" MaximumRowsParameterName="PageSize" SelectCountMethod="GetRowsCount" StartRowIndexParameterName="StartRow" SortParameterName="sortBy">
    </asp:ObjectDataSource>


    <asp:GridView ID="supplierGridView" runat="server" AllowSorting="True" 
         CellPadding="4" DataSourceID="odsSupplier"
        ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="15" 
         Width="300px">

        <Columns>
            <asp:ButtonField ButtonType="button" CommandName="Ed" HeaderText="Edit" 
                Text="Edit">
                <ControlStyle Width="75px" />
                <HeaderStyle HorizontalAlign="Center" Width="75px" />
                <ItemStyle Width="75px" HorizontalAlign="Center" />
            </asp:ButtonField>

            <asp:ButtonField ButtonType="button" CommandName="Del" HeaderText="Delete" 
                Text="Delete">
                <ControlStyle Width="75px" />
                <HeaderStyle HorizontalAlign="Center" Width="75px" />
                <ItemStyle Width="75px" HorizontalAlign="Center" />
            </asp:ButtonField>
        </Columns>

        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerSettings Mode="NumericFirstLast" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
</div>

供应商类

public DataView GetSuppliers(int StartRow, int PageSize)
{
    return GetData(StartRow, PageSize, "EquipmentSupplierID");
}

public DataView GetSuppliers(int StartRow, int PageSize, string sortBy)
{
    return GetData(StartRow, PageSize, sortBy);
}

public DataView GetSuppliers(string sortBy)
{
    return GetData(int.MaxValue, 0, sortBy);
}

private DataView GetData(int StartRow, int PageSize, string sortBy)
{
    dbconn db = new dbconn();
    return db.getSupplierData(StartRow, PageSize, sortBy).DefaultView;
}

public int GetRowsCount()
{
    dbconn db = new dbconn();
    return db.getSupplierCount();
}

dbconn 类

public int getSupplierCount()
{
    open();
    setQuery("SELECT COUNT(*) AS Count FROM equipmentsupplier");
    MySqlDataReader msdr = executeReader();
    msdr.Read();
    int count = int.Parse(msdr["Count"].ToString());
    close();
    return count;
}

public DataTable getSupplierData(int StartRow, int PageSize, string sortBy)
{
    open();
    setQuery("SELECT * from equipmentsupplier ORDER BY ?SORT LIMIT ?StartRow, ?Total;");
    setParameter("StartRow", StartRow);
    setParameter("Total", PageSize);
    setParameter("SORT", sortBy);

    DataTable dt = new DataTable();
    dt.Load(executeReader());
    return dt;
}

当我对数据进行排序时,查询返回了正确的数据,但它只是没有显示在我的 gridview 上。

最佳答案

@user2103743 将所有按钮字段更改为模板字段

关于c# - Gridview 排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15048766/

相关文章:

c# - Fellow Oak DICOM - 更改图像窗口级别

c# - 在不遇到集合修改异常的情况下从列表中删除项目的最有效方法?

c# - 如何将控制台 C# 应用程序添加到 ASP.NET 网站?

c# - Asp.net 标识 : Why the clustered index is on guid (nvarchar) column and how to change this?

c# - ASP.NET : Testing Concurrency

c++ - C 或 C++。如何比较给定 char * 指针的两个字符串?

c - 快速排序的问题

c# - 将 SQL 转换为 LINQ to SQL

asp.net - 什么会在一台服务器上导致此错误,而不是另一台服务器?

python - 如何在python中对日期进行排序?