Tuesday, April 21, 2009

Sorting in a Asp.net Gridview with custom data object


This article explains about how to sort the gridview with the custom object. Here I have used viewstate and no need to any server call for the sorting.
Here I have filled the gridview with all the files or folder within a directory. To get the properties of the file/folder I have created a class FileObject class


public class FileObject
{
private string name;
private string path;
private int size;
private string type;
private string modified;

public string Modified
{
get { return modified; }
set { modified = value; }
}
public string Type
{
get { return type; }
set { type = value; }
}
public int Size
{
get { return size; }
set { size = value; }
}
public string Path
{
get { return path; }
set { path = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}

To store all the object to a collection I have created a custom collection class.
public class DataCollection : IEnumerable
{
private List listData = new List();

public T GetData(int index)
{
return listData[index];
}
public void AddData(T c)
{
listData.Add(c);
}
public void ClearData()
{
listData.Clear();
}
public int Count
{
get { return listData.Count; }
}
// IEnumerable extends IEnumerable, therefore
// we need to implement both versions of GetEnumerator().
IEnumerator IEnumerable.GetEnumerator()
{
return listData.GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return listData.GetEnumerator();
}
public void Add(T c)
{
listData.Add(c);
}

}


To sort the data in the collection I have created a GenericComparer class which is inherited from the IComparer.
public sealed class GenericComparer : IComparer
{

public enum SortOrder { Ascending, Descending };

#region member variables

private string sortColumn;

private SortOrder sortingOrder;

#endregion

#region constructor

public GenericComparer(string sortColumn, SortOrder sortingOrder)
{

this.sortColumn = sortColumn;

this.sortingOrder = sortingOrder;

}

#endregion

#region public property

///
/// Column Name(public property of the class) to be sorted.
///

public string SortColumn
{
get { return sortColumn; }
}

///
/// Sorting order.
///

public SortOrder SortingOrder
{
get { return sortingOrder; }
}

#endregion
#region public methods

///
/// Compare interface implementation
///

/// First Object
/// Second Object
/// Result of comparison
public int Compare(T x, T y)
{
PropertyInfo propertyInfo = typeof(T).GetProperty(sortColumn);
IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);
if (sortingOrder == SortOrder.Ascending)
{
return (obj1.CompareTo(obj2));
}
else
{
return (obj2.CompareTo(obj1));
}
}
#endregion
}

To Sort the gridview I have used the following gridview command

protected void dgrid_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["SortBy"] == null)
{
ViewState["SortBy"] = "DESC";
}

else if (ViewState["SortBy"].ToString().Equals("ASC"))
{
ViewState["SortBy"] = "DESC";
}
else
{
ViewState["SortBy"] = "ASC";
}

BindData(e.SortExpression);
}


Thanks for my colleague Poonam to help me for this.




Download:

1 comment:

  1. Wohhhh!!! pankaj you have done this...and thanks for your help...This is you friend Mayank chaturvedi

    ReplyDelete