GridViev排序问题???
点击时怎么没有反映呢? SortExpression 也设置了! GridViev属性还需要设置什么吗?
代码如下,请大侠看看.
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState [ "Order "]==null )
{
ViewState[ "Order "] = "ASC ";
}
else
{
if (ViewState[ "Order "].ToString() == "ASC {
ViewState[ "Order "] = "DESC ";
}
else
{
ViewState[ "Order "]= "ASC ";
}
}
//数据绑定
SqlConnection con = DB.createconnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand( "select employeeID,lastname,firstname,title,birthdate from employees ", con);
DataSet ds = new DataSet();
sda.Fill(ds, "employees ");
ds.Tables[ "employees "].DefaultView.Sort = e.SortExpression + " " + ViewState[ "Order "].ToString();
this.GridView1.DataSource = ds.Tables[ "employees "].DefaultView ;
this.GridView1.DataBind();
[解决办法]
//aspx文件
<asp:GridView ID= "GridView1 " runat= "server " AllowSorting= "True " ...
要排序的列都要有 SortExpression= "... "
//aspx.cs文件
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
ViewState[ "Order "]= "ASC "; //最好给各默认值
BindGrid();
}
}
排序事件
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState[ "Direction "].ToString() == "DESC ")
ViewState[ "Direction "] = "ASC ";
else
ViewState[ "Direction "] = "DESC ";
//你这里好像忘了重新绑定...
BindGrid();
}