如何一个GridView转换为数据表和排序数据表? [英] How to convert a GridView to DataTable and sort the DataTable?

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

问题描述

我想我的GridView控件转换成一个DataTable。注意:GridView的不具有数据源!
我要排序的数据表,并把它背到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.

感谢您提前。

推荐答案

把你的数据表的ViewState 时你绑定的第一次。

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

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

,然后做像...

and then do like...

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

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

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