有没有办法将DataView转换为DataSource?
我尝试点击我的gridview列。这是代码:
internal static string sort(object _sender, GridViewSortEventArgs _e, string _viewStateSortExpression, string _viewStateSortDirection)
{
string expression = _e.SortExpression;
string direction = getSortDirection(expression, _viewStateSortExpression, _viewStateSortDirection); //ascending by default
GridView gvSrc = (GridView)_sender;
DataView dv = new DataView();
gvSrc.DataSource = dv;
gvSrc.DataBind();
dv = (DataView)((SqlDataSource)gvSrc.DataSource).Select(DataSourceSelectArguments.Empty);
dv.Sort = expression + " " + direction;
return direction;
}
单击列进行排序时出现此错误:
Unable to cast object of type 'System.Data.DataView' to type 'System.Web.UI.WebControls.SqlDataSource'.
答案 0 :(得分:4)
将DataView转换为DataTable :
有一个名为ToTable的方法将DataView转换为DataTable
DataTable dt=dv.ToTable();// this method may expects parameters
将DataTable转换为DataView :
DefaultView是一个返回DataTable的DataView的属性
DataView dv=dt.DefaultView
答案 1 :(得分:0)
试试这个,
DataTable m_DataTable = gvSrc.DataSource as DataTable;
if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
gvSrc.DataSource = m_DataView;
gvSrc.DataBind();
}