score: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();
}
score: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
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- sorting a gridview bound to a linq SP
- creating Linq to sqlite dbml from DbLinq source code
- ASP.net gridview datasource null when sorting
- Gridview sorting in ASP.NET
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- I can't get pagination to show up in every page on a GridView with a list as source
- GridView ObjectDataSource LINQ Paging and Sorting using multiple table query
- Compiling Error with LINQ Sorting Code Using List<T>
- source code for LINQ 101 samples
- Gridview sorting not working with Linq to SQL
- Gridview filter and sorting using LINQ
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- ASP.NET Add cell to Gridview row with string from code behind
- How to apply certain code when checkbox is checked in gridview using LINQ?
- Convert string[] to int[] in one line of code using LINQ
- Sorting a list using Lambda/Linq to objects
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Sorting a List<int>
- What Sorting Algorithm Is Used By LINQ "OrderBy"?
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How to use group by on multiple columns with Max function
- How to rank a list with original order in c#
- What is this query's equivalent in .Net 3.5?
- LINQ: how to get 1 month records
- Can I use a compiled query as a source in a second query?
- LINQ, Visual Basic, & Reflection: capitalization of field names from queries returning anonymous type
- what does a linq query return when no results are found
- C# Is it possible to check if any specific digit is present in an ID?
- How to get mismatch position using Linq or lambda operation on two string array
- Coercing a LINQ result list of KeyValuePair into a Dictionary - Missing the Concept
- Don't working Linq.xml template
- get new list which contain any numeric value from current list using LINQ query
- Split a list of items with generic parameters into separate lists of each generic type
- How to bind combobox to linq result value only?
- Performance and Concept questions in LINQ
- how to extract xml node using LinqtoXml
- Filter List inside List Linq
- Prevent LINQ to SQL from returning child objects
- Making a Count that takes into consideration null check - What should i call it? SafeCount?
- How is managed the iteration here?