I have a DetailsView control which gets data from a IEnumerable custom class.
But I can't get updated values from the control so I can process to update them manually in the database. How can I do that?
Regads,
Gustavo
I suspect it can't work with an IEnumerable... The DetailsView probably needs to be able to access the data source by index (or at least a way to access a specific item of the data source), so I think the source has to implement IList. But I can't find anything about it in the documentation, so that's just a guess...
It seems like some added clarification might be useful here, but some notes:
the DetailsView can work with an IEnumerable source (assuming you're setting DataSource to the enumerable), but it'll bind to the first item fetched from the source.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx#binding_to_data
since it sounds like you're not binding to a data source, you'll need to hook up to an event (ItemUpdated in this case, I'd think) and then do the database update yourself
IMHO, if you can switch to binding against a 'real' data source life will be easier since you can let DetailsView do the heavy lifting for you, but if not, hopefully ItemUpdated will work for you :)
Related
I have a listview and would like to update the text of one of the columns for a specific listviewitem (row).
How would I go about doing this?
Hard to say without any context because there are so many ways you could populate your list!
The generic answer is you bind your list to a collection view which itself binds its source to your viewmodel (or you bind directly to your viewmodel if you don't need CollectionView features).
When you want to modify your list, you make sure you raise a modification notification on your property, and XAML binding will take care of updating everything.
It is really basic stuff on dependency property and binding, you should read more about this topic. MVVM-light is a very light framework that allows you to take care of all kinds of binding-related issues with a very clean and neat flavor. You will also find some very good self-explanatory webcasts from the author of the website about all those topics.
I have a grid (DevExpress XtraGrid, if that matters) which is bound to a LINQ to SQL Entity property.
gridItems.DataSource = purchaseOrder.PendingItemsGrouped;
Well, the grid is being displayed properly,and I can see the purchase items that are pending.
The problem arises when purchaseOrder.PendingItemsGrouped gets changed ... once that happens, the grid does not reflect the changes.
The exact procedure is as following:
The user selects a row from the grid, inserts a serial number on a specific textbox, and then hits enter effectively receiving this item from the purchase order, and inserting it into stock.
inventoryWorker.AddItemToStock( userSelectedItem, serialNumber );
The item gets properly inserted to the inventory, but the grid still shows the item as if it is still awaiting it to be received.
How do I solve this problem?
Do I really need to re-bind the grid so the changes can be reflected?
I even tried instead of:
gridItems.DataSource = ...;
This:
gridItems.DataBindings.Add( new Binding( "DataSource", purchase, "PendingItemsGrouped" ) );
But couldn't solve the problem.
Thank you very much for your time,
Isaac.
OBS:
Re-Binding the Grid works, but my question is ... is that even the proper way of doing things? I feel like I'm miles off the right track.
Calling databind is actually the right approach when you think about how databinding in webforms works. In all examples when databinding to objects, calls to databind happen whenever the collection is modified.
The reason it feels wrong is because its a lot cleaner to use a DataSourceControl, such as a LinqDataSourceControl or ObjectDataSourceControl, where all that stuff is handled for you.
Two things that might help you along this path is when using a LinqDataSourceControl, you might need to override the various -ing methods (Selecting, Inserting, Deleting) etc, in order to add additional filtering and logic.
The other thing that springs to mind is http://multitierlinqtosql.codeplex.com/. Especially the section on Custom ObjectDataSource.
I have not tried it myself but Bindable LINQ allows to achieve it.
I'm trying to add some extra logging to my C# winforms application. I have a few data bound forms that all the database stuff is managed by a binding source and some typed datasets/adapters.
With this setup, it's kind of difficult to tell when something is changed, I'd have to manage each field and keep it's previous value. Is there a way I can hook into the dataset and tell when something is changed? I know datarow's have a RowState enumeration, would that be a good place to start? I looked into the binding source's DataMemberChanged event but it never fired so...
To get the original value of an updated Data value you can do this:
<DataTableRow>[<DataColumn>, DataRowVersion.Original]
To know what has changed look at the DataSet.GetChanges method. The example shows how to get the changes and go through them. I also have an old example here that uses a DataTable and shows how to do a comparison after a merge. It inspects the RowState and shows the changed values etc. It's near the bottom of the page and it's in VB since the OP was using that, not C#. I'm headed out now so I can't provide an equivalent translation but it should be pretty straightforward to glean some useful techniques from.
You can use the RowChanged event, the RowChanging event, or any of the other events raised by a DataTable.
What are some good guidelines to follow when deciding whether or not to databind a control (for example, bind a combobox to a List)?
Are there situations where you should always or never databind a combobox?
How do you decide?
I measure it by how much manipulation of the data and its presentation I will be doing.
If it's light manipulation I'll just bind it, or at most do OnRowDataBound.
If I'm extensivelty maipulating the data, I'll spit it to a DataTable, manipulate it and Databind.
I always eventually databind() however.
Usually, if the data already exists in a datasource somewhere and that data has the potential to be kept up to date for the application's uses (through the application itself or by other systems/processes), I'll databind the control to that datasource.
I read somewhere that one of few weaknesses in data binding model is that you never explicitly handle or create the data object that’s bound to the control and as a result you don’t have a chance to add an extra item. But I’m not sure what is meant by that.
What or how exactly would you be able to add an extra item if you were allow to explicitly create a data object? Would you basically be able to bind control to data source and let the runtime automatically fill the control with data source’s data , and additionally that control could also get an extra item from data source by manually creating it?! How would it be able to do that?!
thanx
When you use the OnItemDataBound event, you have full access to the underlying datasource object via e.Item.DataItem. You can use this data to populate any controls in the ItemTemplate via e.Item.FindControl("controlname"). You can also use functions inside the <%# %> tags to format text or calculate values.
What you have read, in my estimation, is pure crap. Up until the point of binding, I can alter the objects in question. One common scenario, for example, is adding a column to rows in a DataTable object (which is actually a collection of rows and columns). I can, in fact, alter by adding a column (let's say sum) to each row.
I can, with some restrictions on classes, do the same with other types of collections and objects.
After I have bound the object, I can still add items to the output by using the databinding method for a row, so I am still not restricted.
In general, I find those that are expounding this garbage are defending using ASP style code in an ASP.NET page.