如何对gridview进行排序?

时间:2014-01-19 17:52:49

标签: c# asp.net sorting gridview

我有一个班级:

public class CustomInvoice
    {
        public string InvoiceId { get; set; }
        public string Date { get; set; }
        public string RecpDate { get; set; }
        public string Category { get; set; }
        public string TotalValue { get; set; }
        public string InvoiceRef { get; set; }
        public string Client { get; set; }
        public string Status { get; set; }
    }

我用数据填充此类,然后将其绑定到gridview:

List<CustomInvoice> invoices = GetInvoices(url);
InvoiceGrid.DataSource = invoices;         
InvoiceGrid.DataBind();

这会正确显示所有信息,但现在我希望能够对其进行排序,因此我添加了一个按钮,其中包含以下方法:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string expression = "InvoiceId";
        SortDirection direction = SortDirection.Ascending;

        InvoiceGrid.Sort(expression,direction);
    }

我也改变了它的html以允许排序:

<asp:GridView ID="InvoiceGrid" runat="server" AllowSorting="True" OnSorting="gridView_Sorting"></asp:GridView>

然而,当这次运行时,我得到以下异常

System.Web.HttpException (0x80004005): The GridView 'InvoiceGrid' fired event Sorting which wasn't handled.\r\n 

有人能告诉我如何使用按钮的onclick对我的gridview进行排序吗?

谢谢:)

编辑:

我尝试将其添加到列标题中(忽略按钮)

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = InvoiceGrid.DataSource as DataTable;
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + e.SortDirection;

            InvoiceGrid.DataSource = dataView;
            InvoiceGrid.DataBind();
        }

然而,问题是DataTable为null,即使我可以看到invoiceGrid有数据。

2 个答案:

答案 0 :(得分:0)

@hamed shams向我提到了这个问题的解决方案,那就是使用linq(我已经完全忘记了)。

所以我在引用customInvoice时使用linq:

var result = from i in invoices
            orderby i.Category ascending
            select i;

然后我将结果绑定到gridview:

InvoiceGrid.DataSource = result;
InvoiceGrid.DataBind();

答案 1 :(得分:0)

请尝试以下方法:

protected void SearchResults_Sorting(object sender, GridViewSortEventArgs e)
{
    var searchedStudents = this.SearchResults();
    var sortedData = searchedStudents.DefaultView;
    var sortDirection = !this.SortDirections.ContainsKey(e.SortExpression) || !this.SortDirections[e.SortExpression] ? "desc" : "asc";
    var sortDirections = new Dictionary<string, bool>();
    sortDirections.Add(e.SortExpression, sortDirection == "desc");
    this.SortDirections = sortDirections;        
    sortedData.Sort = e.SortExpression + " " + sortDirection;
    this.SearchResults.DataSource = sortedData.ToTable();
    this.SearchResults.DataBind();
}

private Dictionary<string, bool> SortDirections
{
    get
    {
        var sortDirection = new Dictionary<string, bool>();
        if (this.ViewState["SortDirection"] != null)
        {
            sortDirection = (Dictionary<string, bool>)this.ViewState["SortDirection"];
        }

        return sortDirection;
    }
    set
    {
        this.ViewState["SortDirection"] = value;
    }

如果您有任何问题,请与我们联系。