I'm struggling with the same problem at the moment.
How is it possible to 'set the SortExpression and SortDirection' when these are read-only variables?
Cheers
Siggy
PS. I ended up using some code from another thread and basically creating my own flip-flop for asc or desc.
I'm new to asp.net so my example code included here for others my be
way off base, but if anyone else can provide a better answer.... :o)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["sortColumn"] = " ";
ViewState["sortDirection"] = " ";
}
}
protected void gvResults_Sorting(object sender, GridViewSortEventArgs e)
{
string strSearch = tbFind.Text;
string sortDir;
if(ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
{
if ("ASC" == ViewState["sortDirection"].ToString())
{
ViewState["sortDirection"] = "DESC";
//gvResults.SortDirection = SortDirection.Descending;
}
else
{
ViewState["sortDirection"] = "ASC";
//gvResults.SortDirection = SortDirection.Ascending;
}
}
else
{
ViewState["sortColumn"] = e.SortExpression.ToString();
ViewState["sortDirection"] = "ASC";
}
// I need to take into account multi words search, as not done here!!!!
string cmdStr = "SELECT
Author, Year, Title, Creator, Publication, Description, URI FROM
Archive "
+ "WHERE Author LIKE '%" + strSearch + "%' "
+ "OR Title LIKE '%" + strSearch + "%' "
+ "OR Keywords LIKE '%" + strSearch + "%' "
+
"ORDER BY " + ViewState["sortColumn"].ToString() + " " +
ViewState["sortDirection"].ToString();
BindData(cmdStr);
}
void BindData(string sqlCmd)
{
///////////////////////////////////////////////////
// as I'm manually binding dataset to the gridview
// I need to overload the pageindexchanging & sorting
// methods. Using the SqlDataSource wouldn't work
// here as we're using dynamic sql command based on use input!
//////////////////////////////////////////////////
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["PortlandDARConnectionString"].ToString());
SqlDataAdapter da = new SqlDataAdapter(sqlCmd, conn);
ds = new DataSet();
da.Fill(ds, "Results");
gvResults.DataSource = ds;
gvResults.DataBind();
}