开发者

DataTable must be set prior to using DataView

When i try to sort my table manually, i receive this error: DataTable must be set prior to using DataView. The code is:

protected void GridView1_Sorting(object sender, GridViewSort开发者_Python百科EventArgs e)
    {

        DataTable sourceTable = GridView1.DataSource as DataTable;
        DataView view = new DataView(sourceTable);
        string[] sortData = Session["sortExpression"]!= null ? Session["sortExpression"].ToString().Trim().Split(' ') : null;
        if (sortData != null && e.SortExpression == sortData[0])
        {
            if (sortData[1] == "ASC")
            {
                view.Sort = e.SortExpression + " " + "DESC";
                Session["sortExpression"] = e.SortExpression + " " + "DESC";
            }
            else
            {
                view.Sort = e.SortExpression + " " + "ASC";
                Session["sortExpression"] = e.SortExpression + " " + "ASC";
            }
        }
        else
        {
            view.Sort = e.SortExpression + " " + "ASC";
            Session["sortExpression"] = e.SortExpression + " " + "ASC";
        }

    }

Where i wrong?


Most likely GridView1.DataSource is null. You can find a tutorial on Gridview sorting here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx


It's possible your datatable also needs to be part of a dataset.

Similar to

DataSet ds = new DataSet()
DataTable sourceTable = GridView1.DataSource as DataTable;
ds.Tables.Add(sourceTable)
DataView view = new DataView(sourceTable);
string[] sortData = Session["sortExpression"]!= null ? Session["sortExpression"].ToString().Trim().Split(' ') : null;

This is only from weird past experience


This happened to me when I MISTAKENLY tagged a class with the [DataObject] modifier. That meant the entire class had to be 'bindable' as a DataSource, when in fact that was not the case. Removing the tag removed the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜