C# - Sort A Gridview Of Linq Objects Based On A Derived Field?
Feb 16, 2010
I have written a page which uses Linq to query the database and bind the resulting IQueryable to a datagrid. I have a partial class which contains extra properties which derive their values based on other values brought in from the database.
Sorting works fine on fields that are actually in the database but not for the derived fields. When I attempt to sort on such a field I get an error saying "The member 'Trip.Difference' has no supported translation to SQL. how to allow sorting on these derived fields?
Loading data into gridview, retieving data using linq,I have a method that accepts two paramters that would sort specific field and return the object to the gridviewi.e.
passing into the method GenerateData(string sortExp, string sortDir) sortExp = "Name", sortDir = "descending" linq query://works
I have a gridview with several columns, 3 of which I'd like to sort. The source for the data is held in the session.
protected void MyGridHistorySort(object sender, GridViewSortEventArgs e) { var TheColumn = (e.SortExpression).ToString(); TheDataHistory = (List<ViewDataHistoryModel>)Session["SessionDataHistory"]; var test = "data.DataDate"; var NewDataSource = from data in TheDataHistory orderby test select data; MyGridHistory.DataSource = NewDataSource; MyGridHistory.DataBind();
DataDate is a valid column in the list but the orderby statement doesn't work. Ideally, I'd like it to sort with the variable TheColumn by writing something like test = "data."+TheColum; and then add a sort direction based on a boolean. I looked at the OrderBy extension method NewDataSource.OrderBy(test); but that doesn't work either.
Say I have a table Comments with these columns: Id, Comment, Category, CreatedDate, CommenterIdI want to get the top 5 categories from the Comments table (based on the count of each category in that table). How can I do this in linq, to return either List or IQueryable?
I have 2 fields in my gridview one called ScriptType the other BagNo, when a user click the edit button, I would like the BagNo filed to be disabled if the ScriptType field is = "TTA" and enabled otherwise. How can I do this?..something along these lines see below..I am using VB..
I have a gridview which is having coloumns Company name, Industry type, country, employees, revenue. i want to add dropdown at the top of each coloumn so that if user select industry as agriculture, all agriculture industry rows may be sorted, and same for other dropdown also.. i it may be possible,
I have a page which lists all the files in a particular folder (all PDFs), using a data-table and gridview.
I'm currently sorting this table by the filename (by using a dataview), which isn't that helpful, and I want the gridview of files sorted by the file created or file modified date (as recorded in Windows).
If that's not possible, a second option would be to extract the date from the file name string (no problem doing that), and sort the dataview/datatable or gridview based on that. Example Filename: DailySalesReport-1-15-2010. My only hangup with this is how do I sort on date, when it's a string value? Convert to date? How would I sort the whole dataset based on this converted value?
I can't find the answer to this easy question, because I don't know the terminology I am looking to use. I have 2 tables, one that is a Users Table, and another (UserProfiles) that extends the Users table to include more info, like Age, Sex, Weight, etc.I am using the Entity Framework as my model, and the UserProfiles inherits from the Users table on a 1:1 basis.
All I need to do is the learn the syntax, that allows LINQ to Entities to query only some of the data from UserProfiles. For example, maybe I only need Sex and Weight, but not Age.
Just spent about 8 hours googling and looking through this forum for a solution on how to make sure that I can dynamically sort. Here is the situation.
I have 1 Gridview that displays 10 different scenarios based on what button is clicked.
I am also returning only top 10 records. I am doing all of the data binding pragmatically. So I have BoundFields, HyperLinkFields etc.
I also want to sort some records. When I change DataSource SQL statement the gridview somehow remembers what the last sort value was and it errors out saying that value "x" cannot be sorted because it does not exists.
Here I get an error that says that the data source does not support sorting? Doesnt it say gridview1.allowsorting = false;
I also tried gridview1.sort("", SortDirection.Ascending); This does nothin... except query my database one more time because i have a onSorting event that looks like this:
[Code]....
Here is an example of just one of those SLQ statements inside GetSQLQuery:
i have two tables tblProduct and tblCategory, what i am trying to do is populate a gridview, i have LinqDataSource binded to the grid and the correct association made inside the .dbml (tblProduct.CategoryID to tblCategory.ID).
To show the fields from tblCategory instead of the tblProduct.CategoryID in the GridView i am using :
This works fine inside the VS2008 debugger, but once its published to the server (2003 + IIS6.0) the column that belong to tblCategory simply don't show, instead of having the category name (tblCategory.Name) displayed like it does inside the debugger it is blank, no errors simply wont show.
If there's a 0 in the field the code inserts a null. Otherwise it inserts a date. How can I change this to insert a NULL if a question mark "?" appears in the column?
This is what I tied using the OR operator but it's not working:
The Listbox has a list of students. The Gridview has a list of classes offered during a period, such as:
Math Science English etc. When I click a student in the Listbox, I call a sproc via Linq, and get the ClassID and ClassTitle that student is registered for.
What I need to do is Highlight the record in the GridView that matches the ClassID or ClassTitle returned by the sproc.
Previously, I used a ListBox for the Class Offerings. This is easy with a Listbox using the FindByText method:
I need to bind a List control based on the Gridview Row's datakeyName value while the Gridview control is being loaded into the page. I tried the following but it does not work.The BulletedList control is nested inside a Gridview Control and they both bound to an ObjectDataSource control. In my code, I passed teh UserID into the UserID parameter and call the Select() method but still does not work. Basically, I want to list of a user's hobbies, therefore the hobbies control are driven by the userID. tell me how can I get it to work?
protected void UserGridView_RowDataBound(object sender, GridViewRowEventArgs e) { int UserID;
I have a collection of objects that I want to use LINQ in order to filter out only objects that have properties that meet a certain criteria. I am not big on LINQ so I am not sure how to do this.
I create a basic LINQ statement and get the records this way
Dim lr As IEnumerable(Of LogRecord) = From rr In _logRecords where rr.username="something"
but I only want to add that where condition if someone actually filled in a textbox.
I have other conditions to do the same thing with.
How do you reference a LINQ object that you have created such as a list, in your HTML? I have seen examples of how to do this using MVC, but I am using Web Forms. Here is my LINQ :
[Code]....
And I want to be able to for example check if my linq returns any data and if so iterate through it using a foreach loop in my HTML. change my linq so that it returns a generic list rather than what it is doing at the moment.
Private Sub MakeMeSomeXmlBeforeRyanGetsAngry() Dim db As New MyDBDataContext Dim customer = From c In db.Customers Select c Dim dcs As New DataContractSerializer(GetType(Customer)) Dim sb As StringBuilder = New StringBuilder Dim writer As XmlWriter = XmlWriter.Create(sb) dcs.WriteObject(writer, customer) Dim xml As String = sb.ToString Response.Write(xml) End Sub
I am attempting to serialize my linq collection of customers. But it keeps throwing
Type 'System.Data.Linq.DataQuery`1[MyDB.Customer]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.
My issue is that I have already marked the dbml Serialization Mode to UniDirectional and when I check the dbml codebehind all of the DataContract()> and DataMember()> elements are there.
I am not sure how to proceed. I have tried adding various dataloadoptions and setting deferredloading to false, but no luck.
(the user selects the sort field from the Dropdown list ddlSort and the sort direction from the Radiobutton list rbSort).
This works fine, the only problem is that this doesn't work for the formula fields, because they are not part of the database. However, since it is possible to set the sort order by the formula field in design time, it should be possible to do this programmatically.
I am use the new ASP.NET MVC 2 and Entity Framework, but when I try to create a new view with strongly-typed option selected, the entity objects is not appears at dropdown and I cannot use the view creation wizard. I recompile the solution, but still not appears yet. Have any workaround that I forgot to do to enable the entities to be displayed at dropdownlist ?
I am using Linq To Sql to get List of objects with data from database. To bind data with a grid view i am creating a dataset explicitly and then adding List values to that explicitly created dataset.
Now my question is whether it is a good pratice to do what i am doing (in terms of resource utilization) or is there any better way to this...
I have a Details view containing a 2 fields. A Name and a phone number.The Name field is a template with a dropdown list that's popuplated from a database table of Names and Phone numbers. The dropdownlist shows the names. When the user selects a name I want to put the phone number into the second field in the Detailsview.