我有一个如下的gridview:

 <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>


我有一个带有gridview和objectdatasource控件的页面。启用AllowPaging和AllowSorting。这是我使用的获取数据并将objectdatasource绑定到网格的方法:

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


我有一个排序事件,但是当我检查grvBrokerage.DataSource时,它为null。我很好奇为什么?这是代码?

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


这是ObjectDataSource声明:

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


谢谢,
X

最佳答案

使用ObjectDataSource(或任何其他* DataSource)时,请为GridView设置DataSourceID,而不是为DataSource设置。 DataSourceID应该是您的ObjectDataSource的ID。如果您提供ObjectDataSource的声明,我也许可以提供更多帮助。

关于您的Sorting事件中DataSource为什么为null的原因,是因为您设置了DataSource,将页面发送到客户端,单击列标题,然后发布回服务器,现在有了一个全新的GridView实例,其DataSource属性集。旧的GridView实例(以及绑定到的数据表)已被丢弃。

关于c# - 使用ObjectDataSource对GridView进行排序不进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/263477/

10-11 18:03