C# – Sorting a GridView with an ObjectDataSource is not Sorting

asp.netc

I have a gridview like below:

 <asp:GridView DataKeyNames="TransactionID" 
               AllowSorting="True" AllowPaging="True"ID="grvBrokerage" 
               runat="server" AutoGenerateColumns="False" 
               CssClass="datatable" Width="100%"
              <Columns>
                  <asp:BoundField DataField="BrkgAccountNameOutput"              
                                  HeaderText="Account Name"/>
                  <asp:BoundField DataField="TransactionAmount" 
                                  HeaderText="Transaction Amount" 
                                  SortExpression="TransactionAmount" />
                  <asp:BoundField DataField="TransType" 
                                  HeaderText="Transaction Type"          
                                  SortExpression="TransType"/>
                  <asp:BoundField DataField="AccountBalance" 
                                  HeaderText="Account Balance"/>
                  <asp:BoundField DataField="CreateDt" 
                                  HeaderText="Transaction Date"  />
               </Columns>
 </asp:GridView>

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:

        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 > 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();
           }           
      }

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?

  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();
           }
       }

Here is the ObjectDataSource declaration:

<asp:ObjectDataSource ID="ObjectDataSource1" 
                      runat="server" 
                      SelectMethod="GetAllWithId"
                      TypeName="BrokerageTransactions">
                      <SelectParameters>
                          <asp:ControlParameter  
                              ControlID="ddlBrokerageDetails"           
                              Name="brokid" 
                              PropertyName="SelectedValue"
                              Type="Object" />
                      </SelectParameters>
</asp:ObjectDataSource>

Thanks,
X

Best Answer

When you are using an ObjectDataSource (or any other *DataSource), you set the DataSourceID for your GridView, not the DataSource. The DataSourceID should be whatever the ID of your ObjectDataSource is. If you provide the declaration of your ObjectDataSource, I might be able to help more.

As to why your DataSource is null in your Sorting event, it's because you set the DataSource, sent the page to the client, clicked on a column header, posted back to the server, and now have a brand new GridView instance that has never had its DataSource property set. The old GridView instance (and the data table you bound to) have been thrown away.