Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a GridView with an ObjectDataSource is not Sorting

Tags:

c#

asp.net

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

like image 908
Xaisoft Avatar asked Nov 19 '25 23:11

Xaisoft


2 Answers

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.

like image 95
Eddie Deyo Avatar answered Nov 21 '25 13:11

Eddie Deyo


Your datasource no longer exists when you're posting back.

Try creating your datasources in your Page_Init function, then hooking your gridview up to it. Has always worked for me (Using SQLDataSources).

EDIT: Alternatively, you could re-create your datasource for the grid on each postback. That might work.

EDIT2: Or, you could save your DataSource to the ViewState (If it's not that large), then reset the grid to whatever datasource is in the viewstate on postback (again, I stress that the dataset not be magnificently large or else you'll have slow load times)

like image 29
AndyG Avatar answered Nov 21 '25 13:11

AndyG