将列表绑定到DataGrid [英] Binding List to a DataGrid

查看:89
本文介绍了将列表绑定到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.

推荐答案

您需要创建一个实现IComparer接口的类.这是一个很好的教程使用IComparable和IComparer [ ^ ]
希望这对您有帮助
You need to create a class that implements IComparer interface. Here is a good tutorial Custom Sorting With IComparable and IComparer[^]
Hope this helps


尝试此

try this

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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