Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sorting using generic classes + c#

Sorting using generic classes + c#

Scheduled Pinned Locked Moved C#
databasecsharpsql-serversysadminalgorithms
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abcurl
    wrote on last edited by
    #1

    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;
    }

    L 1 Reply Last reply
    0
    • A abcurl

      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;
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups