Flex Datagrid to Array? - apache-flex

I need to convert a datagrid table in Adobe Flex to an ArrayCollection. I was expecting to be able to loop through each row of a datagrid and write that to the Array collection, but the only method for accessing data in the datagrid that I can find is SelectedItem, which doesn't help me.
Obviously one could just copy the dataProvider for the datagrid, but my datagrid is editable and I need to store the state of the datagrid at any one time into a database. Can anyone recommend a method of doing this?
Much Appreciated
-Matt

If your DataGrid is:
<mx:DataGrid id="someDG" dataProvider="{this.provider}" />
Then check if this.provider is Array or ArrayCollection. If it is ArrayCollection then access it simply by:
var gotIt:ArrayCollection = this.someDG.dataProvider as ArrayCollection;
if it is an Array, then:
var gotIt:ArrayCollection = new ArrayCollection(this.someDG.dataProvider as Array);
Hope this helps.

You can do it by iterating over the fields in the columns, but you should be using the data provider. If the only place the state of your data resides is in a transient user interface control then you have to get it back into the domain objects and serialise them. Presumably each row in the datagrid is an object of some sort, in which case I would re-cast your problem as how to keep the edits in the datagrid and the domain objects in sync. If you crack that then there's no need to iterate over the datagrid.
The easiest way to solve the sync problem is to watch events on your editing renderers in the datagrid. When the grid contents change you automatically update the domain object.
What are your in place editors in the grid?

Related

Iterate through RadDataGrid Winrt

I'm using the RadDataGrid controls for Windows 8 and I want to be able to loop through the items in the datagrid. Can it be done
It's difficult to say what you're having problems with without any details in your question.
RadDataGrid is an ItemsControl meaning that it has ItemsSource property to which you bind the collection of items you want the control to show. This collection implements IEnumerable allowing you to iterate through it.
Declaration in XAML:
<telerik:RadDataGrid x:Name="MyDataGrid"/>
Iterating the items collection in code:
foreach (var item in (MyDataGrid.ItemsSource as IEnumerable))
{
// process item as required
}
Since you know the items and the collection you're using, you can cast it to a more specific type. You can even hold a typed reference to the data bound object to avoid the need for casting.

JavaFX refetch observablelist from database

I need to show a custom made list of nodes from a database table that is frequently updated from external sources.
When changes are made to the database, the list needs to be updated on the screen accordingly.
For this I have implemented an ObservableList with an onChanged method that should check for changes and rebuild the custom list.
I my case the onChanged method will never fire when new data is added in the database, only when changes are made on the fly to the already defined ObservableList.
How can i let the ObservableList know when new data exists in the database ?
Thanks in advance!
Given your answer to jkaufmann it seems you need to change your logic.
Instead of rebuilding ObservableList each time you need to calculate the difference with new data and update ObservableList with new/removed data.

Why data binding does not remember the old values?

I have a classic ObjectDataSource and a ListView in my page. The List view just displays some data and when switched to edit template it allows the user to change the values. I want the user to edit just some values -- so I bind just these ones in the edit template.
The problem is that the other values suddenly turn to nulls or 0. I tried to bind all of the values at once and it works fine, but I cannot understand why the old/original values just disappear. Is there any way how to bind the old values?
Thanks for help.
The problem is, that only the data that is included into a round-trip to the server will be available in the postback. That includes all that that is bound to BoundFields, TemplateFields or if the Propertyname is included in the DataKey (or DataKeyNames, don't know right now).
The best approach to fix this, and to keep the overhead to a minimum is to add your primary key to the DataKeyNames collection. This allows you to have access to your custom object that contains an unique identifier and all properties that have just changes.
In your Update Method of the ODS (in your custom class) you now need to retrieve the old object by its unique identifier, manually assign the new values and saves your object back to the database

How to clear the existing list data values

I would like to know how to delete(clear) the existing list data values before binding new values.
I'm using the list for binding data dynamically using http services. When I make a new call to the service, I want the exiting items to be flushed and bind the newly retrieved data to the same list. How can I achieve this?
You can explicitly set the dataProvider to null, or you can use removeAll() on the dataProvider.
You need not clear the data if you are binding httpService.lastResult to list.dataProvider. The data binding will create a new ListCollectionView object (ArrayCollection or XMLListCollection as the case may be) and assign it to list.dataProvider. Old data will be cleared automatically.

Make Gridview interact with something other than properties

We're planning to create a web application where users can build custom "forms," choosing which fields they would like, and how the data in those fields should be represented. Users can then fill out these forms in a DetailsView-like control, thereby creating "documents." The documents can be shown in a DetailsView, or certain fields of several of them can be shown in a GridView. At least, that's the idea.
The problem is that GridView and DetailsView seem to be specifically designed to access Properties on objects that come out of a DataSource. Since we want to have completely arbitrary forms, we can't restrict ourselves to building a class with Properties to represent each field. We have to be able to have any number of dynamically-specified fields on a form.
Is there any way to leverage the existing controls so we don't have to re-implement paging, sorting, and all the other things that GridViews are already set up to do, or will I just have to create my own GridView-like control from scratch?
Edit:
More specifically, the difficulty I am having is in getting inline editing to work on the GridView. For example, let's say that one of the "fields" that is added to a "form" is a calendar field, which should display a date as text in read-only mode, and display a calendar control in edit mode. When the "save" button is clicked, the date selected by the calendar control needs to be saved to the database as the new value for the given field of the given document (i.e. instance of the form). My initial idea was to create a special DataControlField class which, given a form field key, would know how to databind thusly:
FormDocument doc = DataBinder.GetDataItem(cell) as FormDocument;
FormFieldValue fieldValue = doc.FieldValues[FieldKey];
fieldValue.AddReadOnlyControls(cell);
... instead of:
Object dataObject = DataBinder.GetDataItem(cell);
cell.Text = DataBinder.GetPropertyValue(dataItem, FieldKey);
This would probably work for displaying the field values, but if the user tries to edit and save one of the FormDocuments I don't know how I would convince the GridView to do something like this:
doc.FieldValues[FieldKey] = newValue;
Currently, the API for DataControlField uses the ExtractValuesFromCell method to put the property name and value into an IOrderedDictionary. Those values are then applied to the given properties of the objects in the GridView's databound IEnumerable. The problem is, I can't work with properties of an object because in this case the object needs to have a completely arbitrary number of fields.
A GridView can be bound to any object that implements IEnumerable. The advantage of using one of the xDataSource controls is that it can implement paging and sorting for you without any additional code, but you certainly aren't tied to them.
If I understand your question correctly, you do not know the number of columns to display in the GridView until runtime. In that case, I would recommend building an array from your form data and binding the grid to that. You will have to implement paging and sorting yourself.
The DetailsView is not very customizable so you should take a look at the FormView. However, I think you are going to end up dynamically adding controls to whatever container you use.
What you need is totally dynamic GridView. I quess you would have to extend it with the controls ( functionalities ) in your description
Here's what I ended up doing:
I created a new data type that contained a Dictionary of answers, indexed by Field ID.
I created a new type of DataControlField with a FieldId property, which retrieves the proper answer value for that FieldId from the Dictionary mentioned above.
I added data type and data keys properties to this custom DataControlField and overrode the ExtractValuesFromCell method so that it could create a new instance of the answer class and add those values to a Dictionary, which was stored under the property name by which that dictionary would be found in the new data type mentioned in step 1.
I used my own GridView class, used the .NET Reflector to see how the normal GridView calls the ExtractValuesFromCell method, and then changed that so that it would pass the same Dictionary object in to each DataControlField. This way, each field could add to the same Dictionary, rather than replacing the Dictionary that the last one had added under the same property name.
I used a DataFieldGenerator to generate the one of my custom DataControlFields for every field associated with a given form, and I told the GridView to use that DataFieldGenerator to auto-generate its fields.
I set up my ObjectDataSource so that it would know how to save all the answer values from an object of the type mentioned in step 1.
It was tricky, but worthwhile.

Resources