我想将GridView转换为DataTable。注意:GridView没有DataSource! 我想对DataTable进行排序并将其放回GridView,是否可能?重要的是我的GridView必须排序。
提前谢谢。
答案 0 :(得分:3)
第一次绑定时,将DataTable
放入ViewState
。
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