I have quite a few places where I need to bind form controls directly to a backing database. I'm using LINQ to DataSet to do this.
For example, I have a ComboBox with entries filled in by a database query. The problem is of course that all of my data uses numerical IDs and I need to do a table query to translate this into user-readable options.
Normally I would just join the second table and use the combobox DisplayMember to point at the user-readable string column. This doesn't work because after using join or any projections on the query you're (understandably) unable to convert that query into a DataView.
It's hard to believe that this problem isn't run into by everyone who uses DataView. Is there any way to override my form controls' behavior to make them display a function of their value? Like if their Value is v, then they display SomeMethod(v)?
I don't believe what you are trying to accomplish is possible by means of DisplayMember. Its a property, that's how it was designed. However, there are a few other means to accomplish what you want.
1) You could wireup to the ComboBox's Format event. This is the ideal place to change your databound items into human-readable items for display, and really is exactly what you want.
2) You could use LINQ to SQL classes instead, and override the .ToString() portions in the partial classes to display the data how you want. When you databind to a LINQ to SQL object the ComboBox will display the string value of that object.
Since you are already using LINQ to DataSet, I would just wireup to the Format event, though.
I have a pretty simple approach to these things - if it doesn't play nicely, just invent a view-model, i.e. an object layout designed to make mapping between your domain model and the UI plain sailing. This can also encourage you to populate your data in the correct shape for the view-model, rather than for the domain-model; and this often has decent savings in terms of reducing the number of columns (and sometimes rows) fetched, and (perhaps more importantly) avoiding multiple trips to the DB (by fetching exactly the right data, in one hit, in the right shape).
From a view-model, you can then use simple data-binding techniques, as the data you want is now directly available as first-level values on the view-model.
Related
I have a ListView in my application. It holds information belonging to an object. The most important value in the Object is a date value. I need to make sort the ListView items by date like so...
12-12-2018
13-12-2018
14-12-2018
15-12-2018
I can't assume the dates will come like that from the Web Service and they could bring back later ones before the upcoming ones.
I sorted the objects first and passed them into the adapter but felt that might be cumbersome and thought maybe the ListView might do this for me. Is there a better way of doing this or is this the best way? Will the adapter always render the items over each iteration of the list of objects?
Thanks
In this article:
ListView is an adapter view that does not know the details, such as type and contents, of the views it contains.
From Android design pattern, ListView uses Adapter Pattern to display its content. And there is a case talking about this.
Also, there is another sound, ListView is MVC.
Whether Adapter Pattern or MVC, ListView doesn't hold the data by itself, it needs a Model to manage the data, and display it by Adapter.
Is there a better way of doing this or is this the best way?
Sort the data firstly and pass it to Adapter is the best way. ListView can't do it, it is only responsible for displaying the View. And in this way, your ListView will become more easier to maintain and expand, that means if you want change something on your data, you don't need to change anything on your ListView, you just need focus on your Model which hold your data.
I am new to WPF and MVVM and I have a few questions.
I am looking at a "simple" project to start after doing much reading and a few simple apps.
What I want to do is be able to display data from a database table. Seems simple enough and I can do this with a Model based on each field from the database. But what happens if the database developer adds additional fields??? I have to recreate my Model based on the new fields.
So my question is this. How do I read all fields from a database and display them.
can I create a dynamic model from the database rather than a fixed model??
Is MVVM or even WPF the correct format for this type of application?
Thanks in advance.
If you are keen on using that approach, I guess the "simple" way to do it would be to read the data like this: "SELECT * FROM YOURTABLE" and then fill the result into a datatable. That datatable can then serve as the data source for the gridview, which needs to keep it's autogenerateColumns property set to true. This way it would always show all the columns returned, though this is usually not the practice, since you can't template the columns - but for some cases it's good enough.
I have a scenario where currently a telerik radgrid (webforms) is used on a page which currently allows telerik to control the paging, sorting, filtering etc. However I have noticed as this uses the default telerik behavior for these tasks it does it all in memory so nothing is lazy loaded.
In this instance there is a situation where upwards of 300,000 rows (1.2 million at one point), are returned and this seems to re-query every time it is paged, sorted etc which is cripplingly slow.
Now I want to push these concerns to the server side, as currently the db interactions happen via linq to sql so I would like to instead of just using the base (300k query) take the composable IQueryable and then add the filtering, sorting and paging logic in from the telerik control, as the data all seems to be there, I just dont know when I should be intercepting the controls data bindings to force it to only get the data it needs.
From my current investigating there is an OnSortCommand method, which seems to be triggered when a sort is requested, so I assume from there I can get the SortExpression and put that into the linq query, then there is also a FilterExpression which I can use to constrain the results in the query, and then finally there is the PageSize and CurrentPageIndex variables which allow me to work out how many pages I should be bringing back.
So all the data needed to constrain the queries exists there, however I dont know when I should be intercepting the binding process as currently if I were to intercept OnDataBinding and override the DataSource to take into account the filterexpression and paging details (as sorting is not available until a sorting command to my knowledge) then I would expect there to be only a query to pull back 50 rows (or whatever the page size is), however it seems to do the 300k query, then afterwards do the 50 row query, so I am confused as to if I should be intercepting another event or method.
If anyone could point me in a direction of what I should be intercepting/overriding I would be VERY grateful, as the documentation provided by telerik for this is not that great.
== Edit ==
Just to be a bit more specific about the scenario, I am not really wanting to internally create the datasource (advanced databinding) as they need to be re-usable components, so the control is created on the aspx page, then in the code behind on page load it will set the DataSource with an IQueryable object describing the query (the 300k one), then within this inherited control I want to be able to then use the filterexpression, sortexpression, pagesize and pageindex to append to the query. So the data source is provided external to this class.
== Edit 2 ==
Just to draw more attention to the crux of my confusion, one of the most baffling things for me currently is the fact that my grid is doing 3 queries:
one is to get the virtual row count from the IQueryable, that is fine returns the count as expected
the second is the unaltered 300k query, I dont have a clue why this is occuring
the third is the altered query which brings back 50 rows
As I have overridden the OnDataBinding method I do not know where else it is going to pull this data from, as the DataSource is updated before the end of the OnDataBinding to then have the altered IQueryable. So it is at some point doing something in Telerik Land to fetch this data when I dont want it to, as why do a huge query to get all data, then just query the minimal amount later? makes no sense...
Custom Pagging
Custom Sorting :- Please check "RadGrid2" in this link.
Custom Filtering
To achieve this thing i should suggest do not use AdvanaceDataBinding because it is very hard to manage with this.
Please Try to bind Radgird as we bind asp:Gridview.
Friends,
I have about 20-30 datagrids that have different data on all of them. Is there a way that I can sort a datagrid with out writing custom code in each sorting event?
I want some code that all i need to do is pass it in a name of a datagrid, and perhaps a direction.
I don't want to make a call back to the database, but instead, i have a List Each Datagrid will have a different Generics List Value.
Bill.
Yes, use LinqDataSource on all of them and paging and sorting will be taken care of for you without writing a single line of code.
I'm writing a program that functions as an Excel-style dictionary. Basically, it allows the user to add rows, edit rows, search through them, and so on. I use it for storing and studying vocabulary for foreign languages.
I've gotten a version up and running that I'm quite happy with. It uses a BindingList as a data source for a DataGridView in order to track changes and record them back to the BindingList that I use to store all the vocabulary, and the list itself is made up of a custom class I named "Term", that has properties for "English Word", "Spanish Word", "Examples", ect. What it doesn't do is let the user customize the fields, and that's where my problem comes in. It's pretty much "hard-coded" in that even if I'm studying Spanish or French, the Term class is going to be using the property for "Kanji" from Japanese.
I want to be able to have the user type in what fields they want the dictionary to display and keep track of--basically, they should be able to rename and add/remove columns from the DataGridView. My first thought was to implement this as a List, which stores the names of the fields (and accordingly the number of them, by using the List's length). Then, I would have a Word class that has a List property, and each string in the list represents one of the fields. Then I create a BindingList of this Word class, which leaves me with a BindingList of Lists.
When I try to databind my List list to my DataGridView, the grid comes up empty--it apparently has no idea how I want the data to be displayed and I'm having great difficulty figuring out how to tell it to. I'm not even sure if my approach of having a List of Lists is a good way to implement customizable fields, but it's the best I could think of. In any event, can anyone recommend a way to approach this that lets me add the fields to the table, but also tracks changes and pastes them back to the original source? I need the grid to be used as an editing tool for the user to not only add new elements, but also change existing ones.
It's a personal project, but it's driving me a bit crazy. I was up until 5AM last night trying to figure it out and came up empty-handed. Thanks very much for reading!
I've read your post a couple of times. I'm not sure I understand completly. If I don't, please give some details and I'll try to help.
If I had to do a Excel-like DataGridView, I think I'd use an Array. I would create an array of, say, 256 by 256 and put it as DataSource. Then after the user edits, you read the whole DataGrid and rewrite if it differs from the array you originally had.
I think you might be interested in this class:
http://www.codeproject.com/KB/grid/DGVColumnSelector.aspx
It allows the user to dynamically display which columns are shown in the DataGridView