Hello,
this is my code:
[CODE]
<script runat="server">
Dim authors As New DataSet()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
............
End Sub
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
' Retrieve the data source from session state.
Dim dt As DataTable = CType(Session("Sorting"), DataTable)
' Create a DataView from the DataTable.
Dim dv As DataView = New DataView(dt)
If e.SortDirection = SortDirection.Ascending Then
e.SortDirection = SortDirection.Descending
Else
e.SortDirection = SortDirection.Ascending
End If
dv.Sort = e.SortExpression
GridView1.DataSource = dv
GridView1.DataBind()
End Sub[/CODE]
The Problem is that when I click the Header text It always Sort it by Ascending and never reverse it. So I've tried to debug and when I changed manuale the value of SortDirection to descenting it still kept sorting it only ascendint. So is there any posobility
to change the direction of the sorting.
Thank You,
G. Kalchev
-
-
When you debug and add a watch at "e.SortExpression", does it change at all ? I am not yet that familiar with GridView, but i see you change e.SortDirection. Does that also change e.SortExpression?
Try set "dv.Sort = e.SortExpression" in the if/else instead of the e-expression. This is just my first thoughts. dont have software here to try myself.. -
It does not change and I do not know how to change it. When I put the e.sort... in the If - the same reasul. Actually I've noticed that when I put EnableSortingAndPagingCallbacks="True" in the gridview no kind of sorting does work. But that is not the main problem. I just need to make it work in both direction - Acc and desc
-
Try using ViewState to track the sotring:
if(ViewState["SortExp"].ToString().ToUpper() == e.SortExpression.ToUpper())
{
if(ViewState["SortDirection"].ToString().ToUpper() == " ASC")
ViewState["SortDirection"] = " DESC";
else
ViewState["SortDirection"] = " ASC";
}
else
{
ViewState["SortExp"] = e.SortExpression;
ViewState["SortDirection"] = " ASC";
}dv.Sort = ViewState["SortExp"].ToString() + " " + ViewState["SortDirection"].ToString();
dgDataGrid.DataSource = dv;
dgDataGrid.DataBind(); -
thank you guys. I've found the solution(by Ryan Olshan) here it is:
#region Database Code
private void PopulatePublishersGridView()
{
string m_Access_Connection_String = AccessConnectionString();
OleDbConnection m_Access_Connection = new OleDbConnection(m_Access_Connection_String);
string m_Sql_Query_Select;
m_Sql_Query_Select = "SELECT [PubID], [Name], [Company Name], [Address], [City], [State], [Zip], [Telephone], [Fax], [Comments] FROM Publishers ORDER BY [Name] ASC;";
OleDbCommand m_Access_Command = new OleDbCommand(m_Sql_Query_Select, m_Access_Connection);
OleDbDataAdapter m_DataAdapter_Publishers = new OleDbDataAdapter(m_Access_Command);
DataTable m_DataTable_Publishers = new DataTable("Publishers");
m_DataAdapter_Publishers.Fill(m_DataTable_Publishers);
int m_DataTable_RowCount;
m_DataTable_RowCount = m_DataTable_Publishers.Rows.Count;
if (m_DataTable_RowCount > 0)
{
gridViewPublishers.DataSource = m_DataTable_Publishers;
gridViewPublishers.DataBind();
}
}
private string AccessConnectionString()
{
string m_Access_DatabasePath = Server.MapPath("~/App_Data/biblio.mdb");
return String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", m_Access_DatabasePath);
}
#endregion
#region GridView Code
private string GridViewSortDirection
{
get { return ViewState["SortDirection"] as string ?? "DESC"; }
set { ViewState["SortDirection"] = value; }
}
private string GetSortDirection()
{
switch (GridViewSortDirection)
{
case "ASC":
GridViewSortDirection = "DESC";
break;
case "DESC":
GridViewSortDirection = "ASC";
break;
}
return GridViewSortDirection;
}
protected void gridViewPublishers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridViewPublishers.PageIndex = e.NewPageIndex;
gridViewPublishers.DataBind();
}
protected void gridViewPublishers_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable m_DataTable = gridViewPublishers.DataSource as DataTable;
if (m_DataTable != null)
{
int m_PageIndex = gridViewPublishers.PageIndex;
string m_SortDirection = GetSortDirection();
DataView m_DataView = new DataView(m_DataTable);
m_DataView.Sort = e.SortExpression + " " + m_SortDirection;
gridViewPublishers.DataSource = m_DataView;
gridViewPublishers.DataBind();
gridViewPublishers.PageIndex = m_PageIndex;
}
}
#endregion
#region Page Code
protected void Page_Load(object sender, EventArgs e)
{
PopulatePublishersGridView();
}
#endregion
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.