Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting a GridView with an ObjectDataSource is not Sorting
    text
    copied!<p>I have a gridview like below:</p> <pre><code> &lt;asp:GridView DataKeyNames="TransactionID" AllowSorting="True" AllowPaging="True"ID="grvBrokerage" runat="server" AutoGenerateColumns="False" CssClass="datatable" Width="100%" &lt;Columns&gt; &lt;asp:BoundField DataField="BrkgAccountNameOutput" HeaderText="Account Name"/&gt; &lt;asp:BoundField DataField="TransactionAmount" HeaderText="Transaction Amount" SortExpression="TransactionAmount" /&gt; &lt;asp:BoundField DataField="TransType" HeaderText="Transaction Type" SortExpression="TransType"/&gt; &lt;asp:BoundField DataField="AccountBalance" HeaderText="Account Balance"/&gt; &lt;asp:BoundField DataField="CreateDt" HeaderText="Transaction Date" /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>I have a page with a gridview and a objectdatasource control. AllowPaging and AllowSorting is enabled. Here is the method I use that gets the data and binds the objectdatasource to the grid:</p> <pre><code> protected void BindBrokerageDetails() { HomePage master = (HomePage)Page.Master; BrokerageAccount brokerageAccount = new BrokerageAccount(); brokerageAccount.UserID = new Guid(Membership.GetUser().ProviderUserKey.ToString()); ddlBrokerageDetails.DataSource = brokerageAccount.GetAll(); ddlBrokerageDetails.DataTextField = "Account Name"; ddlBrokerageDetails.DataValueField = "Account Name"; ddlBrokerageDetails.DataBind(); if (ddlBrokerageDetails.Items.Count &gt; 0) { BrokerageTransactions brokerageaccountdetails = new BrokerageTransactions(); DataSet ds = BrokerageAccount.GetBrkID2( new Guid(Membership .GetUser() .ProviderUserKey .ToString()), ddlBrokerageDetails .SelectedItem .Text .ToString()); foreach (DataRow dr in ds.Tables[0].Rows) { brokerageaccountdetails.BrokerageId = new Guid(dr["BrkrgId"].ToString()); } ddlBrokerageDetails.SelectedItem.Value = brokerageaccountdetails.BrokerageId.ToString(); grvBrokerage.DataSource = ObjectDataSource1; grvBrokerage.DataBind(); } } </code></pre> <p>I have a sorting event, but when I check the grvBrokerage.DataSource, it is null. I am curious as to why? Here is the code for that?</p> <pre><code> protected void grvBrokerage_Sorting(object sender, GridViewSortEventArgs e) { DataTable dt = grvBrokerage.DataSource as DataTable; if (dt != null) { DataView dv = new DataView(dt); dv.Sort = e.SortExpression + " " + e.SortDirection; grvBrokerage.DataSource = dv; grvBrokerage.DataBind(); } } </code></pre> <p>Here is the ObjectDataSource declaration:</p> <pre><code>&lt;asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllWithId" TypeName="BrokerageTransactions"&gt; &lt;SelectParameters&gt; &lt;asp:ControlParameter ControlID="ddlBrokerageDetails" Name="brokid" PropertyName="SelectedValue" Type="Object" /&gt; &lt;/SelectParameters&gt; &lt;/asp:ObjectDataSource&gt; </code></pre> <p>Thanks, X</p>
 

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