Gridview properties SortExpression and SortDirection not maintaining values when using paging...
1) I am manually binding to a MySQL database
2) Therefore I am manually handling the firing of the Paging and the Sorting events in codebehind.
3) I do a sort on Column. It sorts fine...
4) I click on say Page 4.. The sorting is completely lost but goes to page 4 of the default sorting (as if the page was loaded the first time and no sort was indicated)
I don't understand why the gridview maintains the PageIndex Value and not the SortExpression and SortDirection values when posting back...For example, if I click on Page 4 and then click a new column to sort. The GridView.pageIndex holds it value.
But if I click on a column header for sorting and then click on page 4. The gridview.sortdirection and gridview.sortexpression are reset....
ASPX
[code]
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" />
<asp:UpdatePanel ID="upEmployeeList" runat="server" UpdateMode="conditional" EnableViewState="true">
<ContentTemplate>
<asp:GridView cellpadding=5 ID="gvEmployeeList" runat="server"
AutoGenerateColumns="false
" PageSize="20" AllowSorting="true" AllowPaging="true"
OnPageIndexChanging="gvEmp
loyeeList_
PageIndexC
hanging"
OnSorting="gvEmployeeList_
Sorting"
OnRowCommand="gvEmployeeLi
st_RowComm
and"
EnableViewState="true"
>
<Columns>
<asp:BoundField DataField="EmpId" HeaderText="Emp ID" SortExpression="EmpId" ReadOnly="true" />
<asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name"
ReadOnly="true" />
<asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name
" ReadOnly="true" />
<asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="Manager" ReadOnly="true" />
<asp:BoundField DataField="Division" HeaderText="Division" SortExpression="Division" ReadOnly="true" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department
" ReadOnly="true" />
<asp:BoundField DataField="Floor" HeaderText="Floor" SortExpression="Floor" ReadOnly="true" />
<asp:ButtonField ImageUrl="~/admin/images/g
rid_edit.g
if" CommandName="edit" ButtonType="Image" />
<asp:ButtonField ImageUrl="~/admin/images/g
rid_delete
.gif" CommandName="delete" ButtonType="Image" />
</Columns>
<HeaderStyle CssClass="gridHeader" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Left" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<AlternatingRowStyle BackColor="#F7F7F7" HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:GridView>
</ContentTemplate>
[/code]
c# file
[code]
private string sSortDirection="ASC";
private string sSortExpression="EmpId";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvEmployeeList.DataSource = ds_gvEmployeeList();
gvEmployeeList.DataBind();
}
}
protected DataSet ds_gvEmployeeList()
{
string sSQL = "SELECT a.*, CONCAT(b.Last_Name,', ',b.First_Name) as Manager, d.NAME as Division, c.NAME as Department ";
sSQL = sSQL + " from employee a ";
sSQL = sSQL + " LEFT JOIN Employee b ON a.MGRID=b.EmpID ";
sSQL = sSQL + " LEFT JOIN Department c ON a.DEPTID=c.DEPTID ";
sSQL = sSQL + " LEFT JOIN Division d ON a.DVSNID=d.DVSNID ";
sSQL = sSQL + " ORDER BY " + sSortExpression + " " + sSortDirection;
DataSet dsTemp = new DataSet();
using (OleDbDataAdapter da_gvEmployeeList = new OleDbDataAdapter(sSQL, sConnect)){
da_gvEmployeeList.Fill(dsT
emp, "EmployeeList");
}
return dsTemp;
}
protected void gvEmployeeList_PageIndexCh
anging(Obj
ect sender, GridViewPageEventArgs e)
{
gvEmployeeList.PageIndex = e.NewPageIndex;
gvEmployeeList.DataSource = ds_gvEmployeeList();
gvEmployeeList.DataBind();
}
private string GetSortDirection(string sSortDirection){
switch (sSortDirection)
{
case "ASC":
return "DESC";
case "DESC":
return "ASC";
default:
return "ASC";
}
}
protected void gvEmployeeList_Sorting(Obj
ect sender, GridViewSortEventArgs e)
{
sSortDirection = GetSortDirection(e.SortDir
ection.ToS
tring());
sSortExpression = e.SortExpression;
gvEmployeeList.DataSource = ds_gvEmployeeList();
gvEmployeeList.DataBind();
}
any help is greatly appreciated!
Yours in deepest gratitude,
Angela Law