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