如何在GridView中对数据进行排序 [英] how to sort the data in gridview

查看:60
本文介绍了如何在GridView中对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有网格视图,并且有子控件,例如添加编辑更新和删除
我想在单击任何列名称时对记录进行排序
请帮助我

我的代码是


i have grid view and i have child controls like add edit update and delete
i want to sort the records when i click on any column name
please help me

my code is

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

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

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

    return newSortDirection;
}


protected void gvbgLabel_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        //Response.Write(gvallRecord.DataSource.GetType()); //Add this line
        DataTable dataTable = ((DataSet)Session["dtClass"]).Tables[0];

        if (gvbgLabel.Rows.Count >= 0)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql1(e.SortDirection);

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

    }
    catch (Exception ex)
    {

    }


}



[edit]添加了代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

在此处找到了很好的示例:
good examples are found here: http://msdn.microsoft.com/en-us/library/aa479347.aspx[^]

I use in the aspx:
 <asp:GridView runat="server" ID="grdServiceMessage" AutoGenerateColumns="false" AllowSorting="true"
             PagerSettings-Mode="NumericFirstLast" PagerSettings-Visible="true" PagerSettings-Position="TopAndBottom"
             AllowPaging="true" PageSize="20"
             OnPageIndexChanging="grdServiceMessage_OPIC"
             OnSorting="grdServiceMessage_OS"
             Caption="Messages"
             EmptyDataText="No Valid information found in searching"
            >
......



在后面的代码中:



In code behind:

protected void grdServiceMessage_OS(Object sender, GridViewSortEventArgs e)
    {
        Session["Column"] = e.SortExpression;
        if (rblSortOrder.SelectedValue.StartsWith(" ASC"))
            e.SortDirection = SortDirection.Ascending;
        else
            e.SortDirection = SortDirection.Descending;
        FillGridView(Session["Column"].ToString(),txtFilter.Text, grdServiceMessage.PageIndex);
    }


FillGridview方法:


the FillGridview method:

private void FillGridView(String ColumnNameToSortOn, String Filter, Int32 PageNr)
    {
        List<sqlparameter> parms = new List<sqlparameter>();
        try
        {
            if (txtStartDate.Text.Trim().Equals(String.Empty))
                txtStartDate.Text = DateTime.Now.AddDays(-DateTime.Now.Day).ToShortDateString();
            if (txtEndDate.Text.Trim().Equals(String.Empty))
                txtEndDate.Text = DateTime.Now.ToShortDateString();
            parms.Add(new SqlParameter("@ServiceID", ddlService.SelectedValue));
            parms.Add(new SqlParameter("@StartDate", Convert.ToDateTime(txtStartDate.Text + " 0:00:00")));
            parms.Add(new SqlParameter("@EndDate", Convert.ToDateTime(txtEndDate.Text + " 23:59:59")));
            DataView view = new DataView();
            String ConCatFilter = String.Empty;
            if (!Filter.Equals(String.Empty))
            {
                if (!ColumnNameToSortOn.Equals("lastModified", StringComparison.OrdinalIgnoreCase))
                {
                    ConCatFilter = ColumnNameToSortOn + " Like '%" + Filter + "%'";
                    lblError.Text = String.Empty;
                }
                else
                    lblError.Text = "Filtering on field 'Occurred' is not possible";
            }
            
                
//loads the data from db into dataview component, is my own library
            SyConOSDBClass.WebControls.UpdateDataView(view, "SE_SelMessageData", parms, ColumnNameToSortOn + rblSortOrder.SelectedValue, ConCatFilter);
            if (view.Count.Equals(0)) // Nothing found!
                grdServiceMessage.EmptyDataText = "Search for column '" + ColumnNameToSortOn + "' with Filter '" + ConCatFilter + "' Lead to no results.";
            grdServiceMessage.DataSource = view;
            grdServiceMessage.PageIndex = PageNr;
            grdServiceMessage.DataBind();
            Sesame.StyleSortableGridView(grdServiceMessage);
            Session["Column"] = ColumnNameToSortOn;
            
        }
        catch (Exception err)
        {
            lblError.Text = err.Message;
            ...
        }
        finally
        {
            ....
        }
    }
</sqlparameter></sqlparameter>


在MSDN上 [ ^ ]


可能会有所帮助,

ASP.NET 2.0的GridView示例:对GridView的数据进行分页和排序 [ ^ ]
:)
Might be helpful,

GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView''s Data[^]
:)


这篇关于如何在GridView中对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆