Let me say this again, very clearly: Do not use the index of the
datagrid to identify records in the database.

 

Suppose someone deletes record 3 from the DB? The keys will then be
0,1,2,4,5...  When you put that in a data grid you will have:

Key      DGIndex

0          0

1          1

2          2

4          3          WRONG!

5          4          WRONG!

....

 

Also if the data gets sorted, you are totally screwed.

 

Instead, use the key propertry itself.  Make sure it is in the data
query you use to retrieve the data, and it will be in the selectedItem
object.

var selectedItemKEY:int = adgProducts.selectedItem.KEY;
dataManager.selectProdDataForEdit(selectedItemKEY);

 

But first, what does selectProdDataForEdit() do? 

 

Tracy

 

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of bredwards358
Sent: Friday, May 23, 2008 3:21 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Help me understand how to manipulate dataProviders

 

After doing some research which consisted mainly of searching through
this forum, I noticed a mantra along the lines of manipulate the
dataProvider, not the controls it populates. It makes sense,
especially since without directly manipulating the primary key of the
very first entry in a database , it will read as 1, while the first
index of a dataGrid will read as 0. Right now, I have a mostly
finished AIR form which serves as a way of maintaining a product
table. The dataGrid's provider is an array collection built from the
results of a select statement. To get to the point I'd like some
assistance in streamlining some things to not onlt help this form, but
others in the application which will use the same data access
functionality. For starters, here's how I update data:
Assume that the first primary key in the very first entry of the
database has been edited to read as 0 after it was entered so it syncs
up with the dataGrid.selectedIndex property.
--------------------
//Begins the proceedure to edit a currently existing item when the
edit button is clicked
public function newEdit():void
{
chosenMode = "Edit";
currentState = "Edit Item";
editColumn.visible = false;
deleteColumn.visible = false;
duplicateColumn.visible = false;
this.lblUniqueID.visible = false;
this.rbAll.visible = false;
this.rbBillable.visible = false;
this.rbNonBillable.visible = false;
try
{
var selectedItemIndex:int = adgProducts.selectedIndex;
//Uses another select statement to get the needed information and
passes the results into
//variables which populates the controls instead of an arrayCollection
dataManager.selectProdDataForEdit(selectedItemIndex);
this.lblUniqueID.text = model.editVarOne;
this.txtNewProductID.text = model.editVarTwo;
this.txtNewProductName.text = model.editVarThree;
this.txtNewBarcode.text = model.editVarFour;
this.txtNewUnit.text = model.editVarFive;
this.txtNewQuantity.text = model.editVarSix;
this.txtNewVendor.text = model.editVarSeven;
this.txtNewCost.text = model.editVarEight;
this.txtNewList.text = model.editVarNine;
this.chkNewBillable.selected = model.editVarBool;
this.lblCurrentStatus.text = "Currently " + model.editVarEleven;
}
catch(ex:Error)
{
trace(ex.toString());
trace(ex.getStackTrace());
}
}
private function saveItem(mode:String):void//Differentiates between
whether you want to insert a new item or edit an existing one
{
switch(mode)
{
case "Add_New": insertNew();
break;
case "Edit": editItem();
break;
default: Alert.show("Cannot Determine State","State Unknown")
break;
}
}
//Actually edits an existing item when the save button is clicked
private function editItem():void
{//Takes the information from the controls and sets them equal to
global variables of the same type which
//are then passed into the function which performs the update
model.insertStringOne = this.txtNewProductID.text;
model.insertStringTwo = this.txtNewProductName.text;
model.insertStringThree = this.txtNewBarcode.text;
model.insertStringFour = this.txtNewVendor.text;
model.insertIntOne = parseInt(this.txtNewUnit.text);
model.insertIntTwo = parseInt(this.txtNewQuantity.text);
model.insertNumberOne = Number(this.txtNewCost.text);
model.insertNumberTwo = Number(this.txtNewList.text);
model.insertStringFive = this.cmbNewClass.selectedItem.toString();
model.insertBooleanOne = this.chkNewBillable.selected;
model.insertStringSix = this.cmbStatus.selectedLabel.toString();
model.insertIntThree = parseInt(this.lblUniqueID.text);

dataManager.updateProduct(model.insertStringOne,
model.insertStringTwo, model.insertStringThree, model.insertIntOne,
model.insertIntTwo, model.insertStringFour, model.insertNumberOne,
model.insertNumberTwo, model.insertStringFive,
model.insertBooleanOne, model.insertStringSix, model.insertNewDate,
model.insertIntThree);

cancelOperation();
//Refreshes the datagrid
refreshGrid();
}
--------------------
What I want is to see if there's an easier way to do this via
accessing the ArrayCollection model.prodMaintData to get the values to
edit as opposed to going through another select statement as well as
some other things that might help. I don't totally understand the
functions associated with the ArrayCollection or at least what needs
to be passed into them to get what I need. Thanks in advance.

Brian Ross Edwards
Tech-Connect LLC

 

Reply via email to