本文介绍了将列表绑定到DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataGird显示详细信息.我的datagrid的数据源是一个列表.因此,我无法在网格中使用排序选项.我使用IComparable< item>在商务舱中并使用此方法.

I''m using DataGird to show details. My datagrid''s data source is a list. Therefore I can''t use the sorting option in the grid. I use IComparable<item> in business class and use this method.

public int CompareTo(Item item)
     {
         return ItemCode.CompareTo(item.ItemCode);
     }


因此,我只能对ItemCode进行排序.我该如何改进它以便对给定的字段进行排序.


From this I can only sort to ItemCode. How can I improve this to sort for a given field.

推荐答案


public SortDirection GridViewSortDirection
   {
       get
       {
           if (ViewState["sortDirection"] == null)
           {
               ViewState["sortDirection"] = SortDirection.Ascending;
           }
           return (SortDirection)ViewState["sortDirection"];
       }
       set
       {
           ViewState["sortDirection"] = value;
       }
   }


protected void grd_InterviewCategoryList_Sorting(object sender, GridViewSortEventArgs e)
    {
        IntervewCategory ObjIC = new IntervewCategory();

        DataTable dt = (DataTable)ObjIC.fn_getInterviewCategorylistDetails();
        DataView dv = new DataView(dt);
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            dv.Sort = e.SortExpression + " DESC";
        }

        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            dv.Sort = e.SortExpression + " ASC";
        }

        grd_InterviewCategoryList.DataSource = dv;
        grd_InterviewCategoryList.DataBind();
    }</pre>


这篇关于将列表绑定到DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 21:50
查看更多