在索引更改后保持GridView排序

时间:2014-05-22 13:34:53

标签: c# sql gridview

我得到了gridview,并在标题点击上运行事件排序,如下所示:

 protected void GridViewFoundations_Sorting1(object sender, GridViewSortEventArgs e)
            {
                DataTable dataTable = GridViewFoundations.DataSource as DataTable;

                if (dataTable != null)
                {
                    DataView dataView = new DataView(dataTable);
                    dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                    GridViewFoundations.DataSource = dataView;
                    GridViewFoundations.DataBind();
                }

            }

private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }

现在我有两个问题:

1)我点击了列标题但它只是短路,因为“ASC”任何人都可以告诉我为什么?
2)即使客户更改页面,我如何保持排序?

绑定网格功能(我有一些过滤器)

protected void BindGridFilters()
        {
            if (tb_fondeName.Text != "" && tb_fondeId.Text == "")
            {

                string sqlConnectString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ToString();
                string sqlSelect = "SELECT  cmsDocument.nodeId,text, Max(updateDate) as UpdateDate from cmsContentXml,cmsDocument,cmsContent where cmsContent.nodeId=cmsContentXml.nodeId and cmsDocument.nodeId=cmsContent.nodeId  and templateId=3094 and text like '%" + tb_fondeName.Text + "%' group by cmsDocument.nodeId,text";

                SqlConnection sqlConnection = new SqlConnection(sqlConnectString);
                SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);



                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
                DataTable sqlDt = new DataTable();
                sqlDa.Fill(sqlDt);
                GridViewFoundations.DataSource = sqlDt;
                GridViewFoundations.DataBind();
            }
            if (tb_fondeId.Text != "" && tb_fondeName.Text == "")
            {

                string sqlConnectString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ToString();
                string sqlSelect = "SELECT  cmsDocument.nodeId,text, Max(updateDate) as UpdateDate from cmsContentXml,cmsDocument,cmsContent where cmsContent.nodeId=cmsContentXml.nodeId and cmsDocument.nodeId=cmsContent.nodeId  and templateId=3094 and cmsDocument.nodeId = " + Convert.ToInt32(tb_fondeId.Text) + " group by cmsDocument.nodeId,text";

                SqlConnection sqlConnection = new SqlConnection(sqlConnectString);
                SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);



                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
                DataTable sqlDt = new DataTable();
                sqlDa.Fill(sqlDt);
                GridViewFoundations.DataSource = sqlDt;
                GridViewFoundations.DataBind();
            }
            if (tb_fondeId.Text != "" && tb_fondeName.Text != "")
            {

                string sqlConnectString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ToString();
                string sqlSelect = "SELECT  cmsDocument.nodeId,text, Max(updateDate) as UpdateDate from cmsContentXml,cmsDocument,cmsContent where cmsContent.nodeId=cmsContentXml.nodeId and cmsDocument.nodeId=cmsContent.nodeId  and templateId=3094 and cmsDocument.nodeId = " + Convert.ToInt32(tb_fondeId.Text) + " and text like '%" + tb_fondeName.Text + "%' group by cmsDocument.nodeId,text";

                SqlConnection sqlConnection = new SqlConnection(sqlConnectString);
                SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);



                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
                DataTable sqlDt = new DataTable();
                sqlDa.Fill(sqlDt);
                GridViewFoundations.DataSource = sqlDt;
                GridViewFoundations.DataBind();
            }
            if (tb_fondeName.Text == "" && tb_fondeId.Text == "")
            {
                BindData();
            }
        }

1 个答案:

答案 0 :(得分:0)

创建属性GridSortDirection

protected string GridSortDirection
        {
            get
            {
                if (ViewState["GridSortDirection"] != null)
                {
                    return Convert.ToString(ViewState["GridSortDirection"]);
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                ViewState["GridSortDirection"] = value;
            }
        }

页面加载提供默认排序方向

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       this.GridSortDirection= "ASC";// setting default value 
    }
}

现在在向网格提供数据源并绑定网格之前,添加以下行并像这样绑定

 SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
 DataTable sqlDt = new DataTable();
 sqlDa.Fill(sqlDt);
 //added 2 lines
 DataView dataView = new DataView(dataTable);
 dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(GridSortDirection);
 GridViewFoundations.DataSource = dataView;
 GridViewFoundations.DataBind();

现在改变排序方向创建一个函数

private string ChangeSortDirection(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "DESC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "ASC";
                    break;
            }

            return newSortDirection;
        }

并在GridViewFoundations_Sorting1中像这样调用此函数

protected void GridViewFoundations_Sorting1(object sender, GridViewSortEventArgs e)
            {
                DataTable dataTable = GridViewFoundations.DataSource as DataTable;

                if (dataTable != null)
                {
                    DataView dataView = new DataView(dataTable);
                    this.GridSortDirection=ChangeSortDirection(e.SortDirection);
                    dataView.Sort = e.SortExpression + " " + GridSortDirection;

                    GridViewFoundations.DataSource = dataView;
                    GridViewFoundations.DataBind();
                }

            }