Click here to Skip to main content
15,915,757 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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);
     }

From this I can only sort to ItemCode. How can I improve this to sort for a given field.
Posted
Updated 5-Apr-11 22:49pm
v2
Comments
walterhevedeich 6-Apr-11 4:59am    
I cant understand the code. You passed an Item object but you are returning the ItemCode.CompareTo method. ItemCode is not passed as a parameter in the method. Is this public? Which is which?
Wayne Gaylard 6-Apr-11 5:05am    
This would work if ItemCode is a Value Type, as he is just returning the CompareTo method of the Base Class.
walterhevedeich 6-Apr-11 5:12am    
I see. Thanks for clarifying. :)
Sanyon_d 6-Apr-11 5:30am    
public int CompareTo(Item item)
{
return this.ItemCode.CompareTo(item.ItemCode);
}

You need to create a class that implements IComparer interface. Here is a good tutorial Custom Sorting With IComparable and IComparer[^]
Hope this helps
 
Share this answer
 
try this

MIDL
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900