Search code examples
c#asp.netlinq-to-sql

Sorting in GridView asp Grid


I wanted to sort my asp grid by setting AllowSorting="True". I also added codes to the event, however I can't make it work.

  private void PopulateGridView()
    {
        var a = from c in sample_worker.get()
                select new
                {
                    c.CemID,
                    c.Title,
                    c.Description
                };
        grd_sample.DataSource = a;
        grd_sample.DataBind();

        
    }

this is the code to populate the grid. i added this below the !ispostback..

the code for sorting..

        private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }
    protected void grd_sample_Sorting(object sender, GridViewSortEventArgs e)
    {

        DataTable dataTable = grd_sample.DataSource as DataTable;


        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " +          ConvertSortDirectionToSql(e.SortDirection);

            grd_sample.DataSource = dataView;
            grd_sample.DataBind();
        }

    }

What can I do to fix this? Also I would be able to sort it back and forth? And the if (dataTable != null) is always null.


Solution

  • You have many things wrong:

    1. Your method PopulateGridView binds an IQuerable to your gridview, yet when you handle OnSorting you pretend to be able to get a DataTable as the DataSource of your GridView.

    2. this line: DataTable dataTable = grd_sample.DataSource as DataTable; will always return NULL because your gridview does not keep a reference to the DataSource. You need to refetch the data, sort it and rebind it. In other words, you need to do something like this on grd_sample_Sorting

      protected void grd_sample_Sorting(object sender, GridViewSortEventArgs e)
      {
      var a = from c in sample_worker.get()
                  select new
                  {
                      c.CemID,
                      c.Title,
                      c.Description
                  };
      
          if(e.SortDirection=="ASC")
          {
               if(e.SortExpression=="CemID")
                  a=a.OrderBy(x=>x.CemID);
               else if (e.SortExpression=="Title")
                  a=a.OrderBy(x=>x.Title);
               //And so on...
          }
          else 
          {
               if(e.SortExpression=="CemID")
                  a=a.OrderByDescending(x=>x.CemID);
               else if(e.SortExpression=="Title")
                  a=a.OrderByDescending(x=>x.Title);
               //And so on...
          }
      
          grd_sample.DataSource = a;
          grd_sample.DataBind();
      }
      

    But Frankly, you are probably better off defining a LinqDataSource for your datagridview. LinqDataSource will perform paging, sorting and CRUD operations almost without writing a single line of code. Certainly not for sorting and paging.