简体   繁体   English

如何对GridView数据进行排序

[英]How to sorting the gridview data

See the below code, I am try to sorting Grid view data by calling function provided by the Microsoft vs 2008, but the paging is done well but the sorting process is not work, tell me where should i change the following code and yes i put grid view in updated penal,is there problem regarding to update penal? 看到下面的代码,我试图通过调用Microsoft vs 2008提供的函数对Grid视图数据进行排序,但是分页操作很好,但是排序过程不起作用,请告诉我应该在哪里更改以下代码,是的更新刑罚中的网格视图,是否有关于更新刑罚的问题?

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    showData();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        SqlCommand cmd = new SqlCommand("showData", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0] != null)
        {

            ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + sortType(e.SortDirection);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
    }
}


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

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

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

    return newSortDirection;
}

i think you have to assign dataview to the DataSource property of gridview, not the dataset object. 我认为您必须将dataview分配给gridview的DataSource属性,而不是数据集对象。 like this 像这样

 DataTable dt = ds.Tables[0];

 DataView dv = new DataView(dt); 
 dv.Sort = e.SortExpression + " " + sortType(e.SortDirection);
 GridView1.DataSource = dv;
 GridView1.DataBind();

you are missing a default value for newSortDirection . 您缺少newSortDirection的默认值。

private string sortType(SortDirection sortDirection)
{
    string newSortDirection = "ASC";

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

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

    return newSortDirection;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM