Xaisoft
Xaisoft

Reputation: 46651

Sorting a GridView with an ObjectDataSource is not Sorting

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

Upvotes: 2

Views: 5455

Answers (4)

Sasi
Sasi

Reputation:

Protected Sub gvRevstatus_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvRevstatus.Sorting
    Dim instance As GridView = gvRevstatus Dim cmd As String

    cmd = "SELECT Status,CONVERT(VARCHAR(10),StatusDate,101) AS StatusDate,RevNo,CommentBY FROM tabStatus WHERE ID =" & CInt(lblId.Text.Trim) & _
    " Order by StatusDate"
    dim gvtab as datatable = BOClaim.GVBoundTab(cnstr, cmd, gvstatus) ' my own classto databind
    Dim dv As New DataView(gvtab)
    dv.Sort = e.SortExpression

    gvRevstatus.DataSource = dv
    gvRevstatus.DataBind()
End Sub

Upvotes: 0

Scott Ivey
Scott Ivey

Reputation: 41588

Along with the other 2 answers, another thing to keep in mind is that when using ObjectDataSource, you need to have a parameter on your method that will do the sort. ObjectDataSources just pass a sort expression to your object as a string, and its up to your object to handle the sort on its own. In the ObjectDataSource, you set the SortParameterName to the value of the sort parameter name. See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.sortparametername.aspx for more information.

Upvotes: 2

AndyG
AndyG

Reputation: 41220

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)

Upvotes: 2

Eddie Deyo
Eddie Deyo

Reputation: 5430

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.

Upvotes: 6

Related Questions