Sorting using generic classes + c#
-
Language - c# Database - sql server 2008 DataGridview It is regarding the sorting in the datagridview using generic classes as a datasource of datagridview I need suggestion for my implementation that whether my approach is right or wrong Following are the function.
private void datagridview1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
SetSorting(e.ColumnIndex);
}private void SetSorting(int ColumnIndex)
{
if (ColumnIndex == -1) return;
IList data = null;
try
{
data = (IList<classInfo>)dgvUploadCharts.DataSource;
SortOrder order = SortGlyphDirectionPublication(ColumnIndex);
dgvUploadCharts.DataSource = SortingDataPublication(data, order, ColumnIndex);
dgvUploadCharts.Columns[ColumnIndex].HeaderCell.SortGlyphDirection = order;
}
catch (Exception ex)
{
throw ex;
}
finally
{
data = null;
}
}private IList<classInfo> SortingDataPublication(IList<classInfo> Obj, SortOrder SortOrder, int ColumnIndex)
{
IEnumerable<classInfo> sortedEnum = null;
try
{
if (datagridview1.Columns[ColumnIndex].Name.ToString() == Enumeration.Enum1.ToString())
{
if (SortOrder == SortOrder.Ascending || SortOrder == SortOrder.None)
sortedEnum = Obj.OrderBy(f => f.Enum1);
if (SortOrder == SortOrder.Descending)
sortedEnum = Obj.OrderByDescending(f => f.Enum1);
return sortedEnum.ToList();
}
}
}private SortOrder SortGlyphDirectionPublication(int ColumnIndex)
{
SortOrder order = datagridview1.Columns[ColumnIndex].HeaderCell.SortGlyphDirection;
if (order == SortOrder.None)
return SortOrder.Ascending;
if (order == SortOrder.Ascending)
return SortOrder.Descending;
if (order == SortOrder.Descending)
return SortOrder.Ascending;
return SortOrder.Ascending;
} -
Language - c# Database - sql server 2008 DataGridview It is regarding the sorting in the datagridview using generic classes as a datasource of datagridview I need suggestion for my implementation that whether my approach is right or wrong Following are the function.
private void datagridview1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
SetSorting(e.ColumnIndex);
}private void SetSorting(int ColumnIndex)
{
if (ColumnIndex == -1) return;
IList data = null;
try
{
data = (IList<classInfo>)dgvUploadCharts.DataSource;
SortOrder order = SortGlyphDirectionPublication(ColumnIndex);
dgvUploadCharts.DataSource = SortingDataPublication(data, order, ColumnIndex);
dgvUploadCharts.Columns[ColumnIndex].HeaderCell.SortGlyphDirection = order;
}
catch (Exception ex)
{
throw ex;
}
finally
{
data = null;
}
}private IList<classInfo> SortingDataPublication(IList<classInfo> Obj, SortOrder SortOrder, int ColumnIndex)
{
IEnumerable<classInfo> sortedEnum = null;
try
{
if (datagridview1.Columns[ColumnIndex].Name.ToString() == Enumeration.Enum1.ToString())
{
if (SortOrder == SortOrder.Ascending || SortOrder == SortOrder.None)
sortedEnum = Obj.OrderBy(f => f.Enum1);
if (SortOrder == SortOrder.Descending)
sortedEnum = Obj.OrderByDescending(f => f.Enum1);
return sortedEnum.ToList();
}
}
}private SortOrder SortGlyphDirectionPublication(int ColumnIndex)
{
SortOrder order = datagridview1.Columns[ColumnIndex].HeaderCell.SortGlyphDirection;
if (order == SortOrder.None)
return SortOrder.Ascending;
if (order == SortOrder.Ascending)
return SortOrder.Descending;
if (order == SortOrder.Descending)
return SortOrder.Ascending;
return SortOrder.Ascending;
}abcurl wrote:
I need suggestion for my implementation that whether my approach is right or wrong
The easiest way to determine whether your implementation is right, is by testing it. Well, does it work? Looks readable enough, why are you doubting?
I are Troll :suss: