Data Grid by Dapfor is one of the best performing components used in many applications. The .Net Data Grid supports various CLR’s such as 2.0, 3.0, 3.5 and 4.0. The Data Grid from various other organizations performs quite well when the data is static but the performance level drops when the data is non-static. This is where Dapfor’s .Net Data Grid comes into the picture and performs far better than the others. Performance of the Operations for the dynamic data on the .Net Data Grid such as sorting, filtering, cell highlighting etc is quite high. Any .Net Data Grid application from the Dapfor consumes less memory and system resource. One of such operation is sorting and its information is present in the grid headers.
//Get top-level header
Header header = grid.Headers [0];
header["Product"].SortDirection = SortDirection.Ascending;
header["Price"].SortDirection = SortDirection.Descending;
Using the Column.Sortable property can prevents the user from modifying the sorting through user interface. On the .Net Data Grid sorting is done with the help of IComparable interface. Generally, data objects gives back only simple values like int, double, string, float, TimeSpan etc. These data types are then used by the IComparable interface on the .Net Data Grid. Because of this unique feature, the .Net Data Grid is able to sort from different rows and simple as well as complex data types. The IComparable interface compares simple and complex values and doesn’t take into consideration the formatted stocks which are available in the grid cells. Look at the code below which takes DateTime value to get the understanding of the .Net Data Grid sorting.
class DateSortingExample
{
private readonly DateTime _date;
public DateExample(DateTime date)
{
_date = date;
}
public DateTime Date
{
get { return _date; }
}
}
//Create header and column
grid.Headers.Add(new Header());
grid.Headers[0].Add(new Column("Date"));
//Configure the header
grid.Headers[0].StretchMode = ColumnStretchMode.All;
grid.Headers[0][0].SortDirection = SortDirection.Ascending;
grid.Headers[0]["Date"].Format = new StringFormat("d", new CultureInfo("fr-FR"));
//Populate grid with random data
Random random = new Random();
BindingList<DateExample> source = new BindingList<DateExample>();
for (int i = 0; i < 6; ++i)
{
source.Add(new DateExample(DateTime.Now + TimeSpan.FromDays(random.Next(1000))));
}
grid.DataSource = source;
It is possible to use user-defined sorting method using the ICustomSort interface to set the rules for sorting on the .Net Data Grid.
//An implementation ICustomSort that sorts values by day of week
class DayOfWeekCustomSort : ICustomSort
{
public int Compare(Row row1, Row row2, string fieldId, object value1, object value2, int defaultResult)
{
if(fieldId == "Date")
{
DateTime d1 = (DateTime) value1;
DateTime d2 = (DateTime) value2;
if (d1.DayOfWeek != d2.DayOfWeek)
{
return d1.DayOfWeek > d2.DayOfWeek ? 1 : -1;
}
}
return defaultResult;
}
}
//Setup custom sorting
grid.CustomSort = new DayOfWeekCustomSort();
One of the great things about the .Net Data Grid is its ability to sort single and multiple columns. Another powerful feature of the .Net Data Grid is its capability to maintain independent as well as simultaneous sorting, filtering and grouping of the data.
//Get top-level header
Header header = grid.Headers [0];
header["Product"].SortDirection = SortDirection.Ascending;
header["Price"].SortDirection = SortDirection.Descending;
Using the Column.Sortable property can prevents the user from modifying the sorting through user interface. On the .Net Data Grid sorting is done with the help of IComparable interface. Generally, data objects gives back only simple values like int, double, string, float, TimeSpan etc. These data types are then used by the IComparable interface on the .Net Data Grid. Because of this unique feature, the .Net Data Grid is able to sort from different rows and simple as well as complex data types. The IComparable interface compares simple and complex values and doesn’t take into consideration the formatted stocks which are available in the grid cells. Look at the code below which takes DateTime value to get the understanding of the .Net Data Grid sorting.
class DateSortingExample
{
private readonly DateTime _date;
public DateExample(DateTime date)
{
_date = date;
}
public DateTime Date
{
get { return _date; }
}
}
//Create header and column
grid.Headers.Add(new Header());
grid.Headers[0].Add(new Column("Date"));
//Configure the header
grid.Headers[0].StretchMode = ColumnStretchMode.All;
grid.Headers[0][0].SortDirection = SortDirection.Ascending;
grid.Headers[0]["Date"].Format = new StringFormat("d", new CultureInfo("fr-FR"));
//Populate grid with random data
Random random = new Random();
BindingList<DateExample> source = new BindingList<DateExample>();
for (int i = 0; i < 6; ++i)
{
source.Add(new DateExample(DateTime.Now + TimeSpan.FromDays(random.Next(1000))));
}
grid.DataSource = source;
It is possible to use user-defined sorting method using the ICustomSort interface to set the rules for sorting on the .Net Data Grid.
//An implementation ICustomSort that sorts values by day of week
class DayOfWeekCustomSort : ICustomSort
{
public int Compare(Row row1, Row row2, string fieldId, object value1, object value2, int defaultResult)
{
if(fieldId == "Date")
{
DateTime d1 = (DateTime) value1;
DateTime d2 = (DateTime) value2;
if (d1.DayOfWeek != d2.DayOfWeek)
{
return d1.DayOfWeek > d2.DayOfWeek ? 1 : -1;
}
}
return defaultResult;
}
}
//Setup custom sorting
grid.CustomSort = new DayOfWeekCustomSort();
One of the great things about the .Net Data Grid is its ability to sort single and multiple columns. Another powerful feature of the .Net Data Grid is its capability to maintain independent as well as simultaneous sorting, filtering and grouping of the data.