如何将 GridView 转换为 DataTable 并对 DataTable 进行排序? [英] How to convert a GridView to DataTable and sort the DataTable?

查看:21
本文介绍了如何将 GridView 转换为 DataTable 并对 DataTable 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的 GridView 转换为 DataTable.注意:GridView 没有数据源!我想对 DataTable 进行排序并将其放回 GridView,这可能吗?重要的是我的 GridView 必须排序.

I want to convert my GridView to a DataTable. Note: The GridView doesn't have a DataSource! I want to sort the DataTable and put it back to the GridView, is it possible? Important is that my GridView must be sorted.

提前致谢.

推荐答案

当你第一次绑定时,把你的 DataTable 放在 ViewState 中.

Put your DataTable in a ViewState when you bind for the first time.

gridView1.DataBind();
ViewState["dtbl"] = YourDataTable

然后喜欢...

protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = ViewState["dtbl"] as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

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

private string ConvertSortDirection(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;

    switch (sortDirection)
    {
        case SortDirection.Ascending:
            newSortDirection = "ASC";
            break;

        case SortDirection.Descending:
            newSortDirection = "DESC";
            break;
    }

    return newSortDirection;
}

另请看这篇 MSDN 文章 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx

Also take a look at this MSDN article http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx

这篇关于如何将 GridView 转换为 DataTable 并对 DataTable 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆