2

I have a gridview and its data source comes from linq to sql statements:

var query = from user in dataContext.tbl_files
            select new { user.File_Name, user.Upload_Time, user.Uploaded_By };

GridView1.DataSource = query.ToList();
GridView1.DataBind();

I am trying to implement sorting feature of gridview :

public void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    string previousSortExpression = (string)ViewState["SortDirection"];
    string sortExpression = e.SortExpression;
    SortDirection sortDirection = e.SortDirection;
    if (sortExpression.Equals(previousSortExpression))
    {
        sortDirection = SortDirection.Descending;
        ViewState["SortDirection"] = string.Empty;
    }
    else
        ViewState["SortDirection"] = sortExpression;

    string direction = sortDirection == SortDirection.Ascending ? "ASC" : "DESC";
    e.SortExpression = string.Format("it.{0} {1}", e.SortExpression, direction);

    DataTable dataTable = (DataTable)GridView1.DataSource; // here returns null!

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression ;
        GridView1.DataSource = dataView;
        GridView1.DataBind();
    }
}

But " DataTable dataTable = (DataTable)GridView1.DataSource; " line returns null however count of datasource is 4. I got this error :

{"Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType03[System.String,System.Nullable`1[System.DateTime],System.String]]' to type 'System.Data.DataTable'."}

How can I sort my gridview and correct my mistake? Thanks..

1
  • Why you are not using a linq2sql datasource, thisway your sorting works out of the box? Commented Jan 3, 2012 at 21:42

2 Answers 2

2

You can't use the DataSource property of the grid to fetch the data source. It is only available after it is set and to the end of that postback. You'll need the fetch the data from the database again.
Something like this:

public void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
  var users = (from user in dataContext.tbl_files
              select new { user.File_Name, user.Upload_Time, user.Uploaded_By }).ToList().AsEnumerable();

  switch(e.SortExpression)
  {
     case "File_Name":
       users = users.OrderBy(x => x.File_Name);
       break;
     case "Upload_Time":
       users = users.OrderBy(x => x.Upload_Time);
       break;
     case "Uploaded_By":
       users = users.OrderBy(x => x.Uploaded_By);
       break;
  }
  if(e.SortDirection == SortDirection.Descending)
    users = users.Reverse();

  GridView1.DataSource = users;
  GridView1.DataBind();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Above answer is correct first you need to assign datasource to gridview again as sorting event is called.

So have to store the data somewhere.

For retrieving the datasource from grid you can follow below steps

The problem lies as you are assigning linq result as datasource of gridview and then taking datatable from the gridview datasource.

Try this code

BindingSource bs = (BindingSource )Gv.DataSource;
DataTable Dt = (DataTable ) bs.DataSource;

Contact if you have any doubt

1 Comment

BindingSource is not recognizable. Should we add any namespace ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.