1

GridView我正在研究一个类来在 C# 中对我的 's 进行数据绑定......

我已经做到了,所以我的寻呼机可以工作,但不确定我将如何进行排序工作......到目前为止,我的代码如下所示:

public static class Binder
{
public class gridView {
    public static void Bind(GridView grid, IList source, bool pager, bool sorting) {
        grid.DataSource = source;
        if (pager) {
            grid.PageIndexChanging += new GridViewPageEventHandler(PageIndexChanging);
        }
        if (sorting) {
            grid.Sorting += new GridViewSortEventHandler(Sorting);
        }
        grid.DataBind();
    }

    public static void PageIndexChanging(object sender, GridViewPageEventArgs e) {
        if (sender != null) {
            GridView GridView1 = sender as GridView;
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
    }
    public static void Sorting(object sender, GridViewSortEventArgs e) {
        if (sender != null) {
            GridView GridView1 = sender as GridView;
            //What to do?
            GridView1.DataBind();
        }
    }
}
}

我会像这样绑定一个gridview:Binder.gridView.Bind(MyGridView, MyDataSource, true, true);

但是我有点迷失了如何进行分类?

4

2 回答 2

0

您需要在您的 aspx 中启用排序 - AllowSorting="True"

<asp:GridView ID="grid" runat="server" DataKeyNames="id" AutoGenerateColumns="False"
                AllowPaging="True" AllowSorting="True" PageSize="25" CellPadding="4" BorderStyle="None"
                OnRowDataBound="gvProfileSearch_RowDataBound" Width="100%" Translate="true">

对于 IList,您可以使用 OrderBy-Function

IList<T> tmp = new IList<T>();
tmp.OrderByDescending...
tmp.OrderBy...
于 2013-03-14T12:01:59.463 回答
0

此代码用于绑定、分页和排序......

 protected void RadgvData_SortCommand(object sender, GridSortCommandEventArgs e)
        {
            GridTableView tableView = e.Item.OwnerTableView;
            e.Canceled = true;
            GridSortExpression expression = new GridSortExpression();
            expression.FieldName = e.SortExpression;
            if (tableView.SortExpressions.Count == 0 || tableView.SortExpressions[0].FieldName != e.SortExpression)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
            {
                expression.SortOrder = GridSortOrder.Ascending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }

            tableView.SortExpressions.AddSortExpression(expression);
            RadgvData.Rebind();

        }

        protected void RadgvData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            try
            {
                DataTable dtData = objCutTypeBAL.getCutTypeDetail(string.Empty);
                if (dtData.Rows.Count > 0)
                {
                    RadgvData.DataSource = dtData;
                }
                else
                {
                    RadgvData.DataSource = String.Empty;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void RadgvData_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                RadgvData.PagerStyle.AlwaysVisible = true;
            }
            if (e.Item is GridDataItem && e.Item.OwnerTableView.DataMember != "ChildCutType")
            {
                GridDataItem dataitem = (GridDataItem)e.Item;

                ImageButton imgDel = (ImageButton)dataitem["Delete"].Controls[0];
                string strCutType;
                strCutType = dataitem.GetDataKeyValue("CutType").ToString();
                DataTable dtData = objCutTypeBAL.getCutTypeDetail(strCutType);
                if (dtData.Rows.Count > 0)
                {
                    imgDel.Visible = false;
                }
            }
        }

        protected void RadgvData_PageIndexChanged(object sender, GridPageChangedEventArgs e)
        {
            RadgvData.CurrentPageIndex = e.NewPageIndex;
            popGrid();
        }
于 2013-04-16T09:38:09.003 回答