Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because you're setting a <code>DataSet</code> as <code>DataSource</code> and then casting it to <code>DataTable</code> with the operator <a href="http://dotnetbutchering.blogspot.com/2008/08/net-difference-between-casting-and-as.html" rel="nofollow noreferrer"><code>as</code></a>. </p> <p>The 'as' operator in C# is a tentative cast - if it's impossible to cast (types are not compatible) instead of throwing an exception as the direct cast the 'as' operator sets the reference to null.</p> <p>If you only have one datatable in your dataset then you can get the first element like this:</p> <pre><code>ds.Tables[0]; </code></pre> <p>... or use the name of the table:</p> <pre><code>ds.Tables["myTable"]; </code></pre> <p>in your case you can try...</p> <pre><code>DataTable dt = GridView1.DataSource.Tables[0] as DataTable; </code></pre> <p>Hope it helps!</p> <p><strong>EDIT</strong>:</p> <p>with regards to your sorting problem (once you get the datatable):</p> <pre><code>if (dt != null) { dt.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); GridView1.DataBind(); } </code></pre> <p>You can do this because dt is a reference to the exact same object that's already set as DataSource for your grid. It should do the trick - if not there's smt else I am missing (such as we're sorting the wrong table meaning you have more than one table in the DataSet).</p> <p><strong>EDIT:</strong></p> <p>had a look at your code. I don't know exactly when GetValues gets fired but I suspect it's causing your problem (I think it might be overriding your sorting or smt along those lines).</p> <p>If you comment out FillDataSource from getValues and modify your PageLoad to do this:</p> <pre><code> void Page_Load(Object sender, EventArgs e) { // Load data only once, when the page is first loaded. if (!IsPostBack) { Session["myDataSet"] = FillDataSet(); } } </code></pre> <p>then in your sort method you retrieve the DataSource like this:</p> <pre><code>DataTable dt = ((DataSet)Session["myDataSet"]).Tables[0]; </code></pre> <p>Also you can retrieve the DataSet from session in your pagination handler method.</p> <p>You should also notice an improvement in performance since you're retrieving the stuff form the db just once.</p> <p>Give it a shot!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload