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 Query
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- ASP NET CORE Entity Framework Select with GroupBy Id
- Shuould we perform LINQ directly in asp net mvc views
- sorting a gridview bound to a linq SP
- creating Linq to sqlite dbml from DbLinq source code
- Running different code depending on what Net Framework version is installed
- 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
- ASP NET MVC VB if searchstring is empty do or else LINQ
- Asp net MVC 5 and listbox with linq
- Compiling Error with LINQ Sorting Code Using List<T>
- source code for LINQ 101 samples
- ASP GridView DataBind with Entity Navigated property
- Group By Asp net core
- Sorting a query based on a category preference using LINQ/Lambda in asp MVC
- Gridview sorting not working with Linq to SQL
- Gridview filter and sorting using LINQ
- ToListAsync() fails on ASP NET CORE but ToList() works - Error "Sequence contains no elements."
- List or Array of String Contain specific word in Html Source Code
- Convert SQL to Linq in ASP Net Core
- Remote SQL SERVER run slow with asp net application
- c# Linq or code to extract groups from a single list of source data
- Linq sum in Controller and pass to View ASP NET CORE 2.1
- Left join two datatables asp net c# LINQ
- Read groupname for specific user in asp net linq
- Linq to SQL: order an asp gridview objectdatasource by an arbitrary column name
More Query from same tag
- linq group by two columns and get only rows with same group by values
- Linq Aggregate on object and List
- Binding Linq object property to combobox
- How can I convert Linq results to DTO class object without iteration
- Create a predicate builder for (x => listOfInts.Contains(x.ListOfIntsToCheck))
- How to find an element with the lowest value by using LINQ?
- How can I group in linq based on the number of elements or an aggregate sum at the same time?
- WCF service big Linq response
- Nested Linq Group By
- get key value pairs from xml using linq
- LINQ Any - Always returning false
- foreach after Linq query on a datatable not working
- Missing schema in DBML if using LINQ to SP and Sp returning multiple record sets
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- Build Where Clause (not sure PredicateBuilder will work for me)
- For loop on Data table on condition
- LINQs Joins vs Stored Procedures Joins
- In C# problem grouping by value and then summing
- Unable to use FirstOrDefault on SqlParameterCollection
- Use LINQ to group multiple columns & split data into several lists
- Is there an easy way for me to iterate through a collection and create a report?
- ASP.NET MVC & Entity - Loading Navigation Properiies/Objects When Using Raw SQL
- Are LINQ expression trees Turing complete?
- Recursive Filtering Linq To Objects
- Linq with a Dictionary inside the class
- How To Count Associated Entities using Where In Entity Framework
- Using LinqKit to Apply a Query to a Single Complex Property
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- Entity Framework AsNoTracking breaks call to Distinct
- Proper way to Categorize by a Property