GridView Sort Event Not firing
Hi,
The Sort Event is not firing inside my grid view. The function
gridViewBillHistory_Sorting(object sender,
GridViewSortEventArgs e) never gets executed. I am calling this function as below
DataTable dtBillHistory = getTable(lSelectedAccountNumber);
gridViewBillHistory.DataSource = dtBillHistory;
gridViewBillHistory.DataBind();
gridViewBillHistory.AllowSorting =
true;
gridViewBillHistory.EnableSortingAndPagingCallbacks =
true;
gridViewBillHistory.EnableViewState =
true;
gridViewBillHistory.Sorting +=
new
GridViewSortEventHandler(gridViewBillHistory_Sorting);
this.Controls.Add(gridViewBillHistory);
The following is the code written inside the .acsx file
<%@
Control
Language="C#"
AutoEventWireup="true"
Inherits="NWC.Internet.Controls.BillHistory, NWC.Internet.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8617c92b638e1ccc"
%>
<script
runat="server">
protected
void gridViewBillHistory_SelectedIndexChanged(object
sender, EventArgs e)
{
}
</script>
<table
border="0">
<tr>
<td
style="margin-left:
40px">
<asp:Label
ID="lblAccountNumber"
runat="server"
Text="<%$
Resources:NWCLiterals, AccountNumber %>"></asp:Label>
</td>
<td
style="margin-left:
40px">
<asp:DropDownList
ID="ddAccountNumber"
runat="server"
CssClass="lblNorm"
AutoPostBack="true"
Width="200"
OnSelectedIndexChanged="ddAccountNumber_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td
style="margin-left:
40px">
<asp:GridView
ID="gridViewBillHistory"
runat="server"
AllowSorting="true"
AllowPaging="true"
PageSize="1"
OnSelectedIndexChanged="gridViewBillHistory_SelectedIndexChanged"
AutoGenerateColumns="false"
OnSorting="gridViewBillHistory_Sorting">
<Columns>
<asp:BoundField
DataField="Bill Number"
HeaderText="Bill Number"
SortExpression="Bill Number">
</asp:BoundField>
<asp:BoundField
DataField="Issue Date"
HeaderText="Issue Date"
SortExpression="Issue Date"
>
</asp:BoundField>
<asp:BoundField
DataField="Amount"
HeaderText="Amount"
SortExpression="Amount"></asp:BoundField>
<asp:BoundField
DataField="Payment Date"
HeaderText="Payment Date"
SortExpression="Payment Date">
</asp:BoundField>
<asp:BoundField
DataField="Payment Status"
HeaderText="Payment Status"
SortExpression="Payment Status">
</asp:BoundField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
Please advice.
The Complete code for the .cs file is given below.
namespace NWC.Internet.Controls
{
public class BillHistory:UserControl
{
protected DropDownList ddAccountNumber;
GridView gridViewBillHistory = new GridView();
long lSelectedAccountNumber;
BillManager IBillManager;
List<NWC.Internet.DAL.BusinessObject.BillHistory> spBillHistoryList;
List<NWC.Internet.DAL.BusinessObject.CustomerAccount> spCustomerAccountRecords;
CustomerManager ICustomerManager;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
try
{
spBillHistoryList = new List<NWC.Internet.DAL.BusinessObject.BillHistory>();
spCustomerAccountRecords = new List<NWC.Internet.DAL.BusinessObject.CustomerAccount>();
string languageCode=string.Empty;
if(CultureInfo.CurrentCulture.Equals("ar-SA"))
{
languageCode="ARA";
}
else
{
languageCode="ENG";
}
ICustomerManager = new CustomerManager();
spCustomerAccountRecords = ICustomerManager.GetCustomerAccount(1219634666, "ENG");
ListItem listSelectedAccountNumber;
if (!IsPostBack)
{
foreach (NWC.Internet.DAL.BusinessObject.CustomerAccount element in spCustomerAccountRecords)
{
string strSelectedAccountNumber = element.accountID.ToString();
listSelectedAccountNumber = new ListItem(strSelectedAccountNumber, strSelectedAccountNumber);
ddAccountNumber.Items.Add(listSelectedAccountNumber);
}
}
lSelectedAccountNumber = Convert.ToInt64(ddAccountNumber.SelectedValue);
DataTable dtBillHistory = getTable(lSelectedAccountNumber);
gridViewBillHistory.DataSource = dtBillHistory;
gridViewBillHistory.DataBind();
gridViewBillHistory.AllowSorting = true;
gridViewBillHistory.EnableSortingAndPagingCallbacks = true;
gridViewBillHistory.EnableViewState = true;
gridViewBillHistory.Sorting += new GridViewSortEventHandler(gridViewBillHistory_Sorting);
this.Controls.Add(gridViewBillHistory);
}
catch(Exception ex)
{
}
}
protected void gridViewBillHistory_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
ViewState["z_sortexpresion"] = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, "DESC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, "ASC");
}
}
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}
private void SortGridView(string sortExpression, string direction)
{
lSelectedAccountNumber = Convert.ToInt64(ddAccountNumber.SelectedValue);
DataTable dtable = getTable(lSelectedAccountNumber);
DataView dv = new DataView(dtable);
dv.Sort = sortExpression + " " + direction;
this.gridViewBillHistory.DataSource = dv;
gridViewBillHistory.DataBind();
}
public DataTable getTable(long lSelectedAccountNumber)
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("Bill Number", typeof(long)));
dt.Columns.Add(new DataColumn("Issue Date", typeof(DateTime)));
dt.Columns.Add(new DataColumn("Amount", typeof(long)));
dt.Columns.Add(new DataColumn("Payment Date", typeof(DateTime)));
dt.Columns.Add(new DataColumn("Payment Status", typeof(string)));
IBillManager = new BillManager();
spBillHistoryList = IBillManager.GetBillHistory(lSelectedAccountNumber, "ENG");
foreach (NWC.Internet.DAL.BusinessObject.BillHistory element in spBillHistoryList)
{
DataRow drBillHistory = dt.NewRow();
drBillHistory["Bill Number"] = element.billID;
drBillHistory["Issue Date"] = element.issueDate;
drBillHistory["Amount"] = element.billBalance;
drBillHistory["Payment Date"] = element.paymentDate;
drBillHistory["Payment Status"] = element.paymentStatus;
dt.Rows.Add(drBillHistory);
}
return dt;
}
protected void ddAccountNumber_SelectedIndexChanged(object sender, EventArgs e)
{
lSelectedAccountNumber = Convert.ToInt64(ddAccountNumber.SelectedValue);
DataTable dtBillHistory = getTable(lSelectedAccountNumber);
gridViewBillHistory.DataSource = dtBillHistory;
gridViewBillHistory.DataBind();
this.Controls.Add(gridViewBillHistory);
}
}
March 18th, 2011 3:09am
Hi,
According to your description, you are likely using an ObjectDataSource and then set AllowSorting to true or you are binding to directly your GridView in Page_Load using something like this. I have found this link about the GridView ‘GridView1’ fired event sorting which wasn’t handle. Please refer to it:
http://justgeeks.blogspot.com/2009/06/gridview-fired-event-sorting-which-wasn.html
Best Regards
David Hu
Free Windows Admin Tool Kit Click here and download it now
March 21st, 2011 9:50pm
Hi,
According to your description, you are likely using an ObjectDataSource and then set AllowSorting to true or you are binding to directly your GridView in Page_Load using something like this. I have found this link about the GridView ‘GridView1’ fired event sorting which wasn’t handle. Please refer to it:
http://justgeeks.blogspot.com/2009/06/gridview-fired-event-sorting-which-wasn.html
Best Regards
David Hu
March 21st, 2011 9:50pm