Dispatching DataGridEvent to enter ItemEditor in DataGrid

I our previous example we made a custom DataGrid, populated it and add functionality to add new data. When we press the Add new city button in our application the new row is selected, but wouldn’t it be cool if we also entered into the Item Editor of the name of the newly added city? That is something that users would probably expect from our program and since we have to change the name of the city we can save one precious click.

Link to application

When I wanted to do this, I Googled my way on to web page from Adobe that explains what event needs to be dispatched if we want to enter Item Editor of a given row and column in data grid. (DataGridEvent).

There is one event that exactly suites our purpose at that is ITEM_EDIT_BEGINNING, The DataGridEvent.ITEM__EDIT_BEGINNING constant defines the value of the type property of the event object for a itemEditBeginning event, which indicates that the user has prepared to edit an item, for example, by releasing the mouse button over the item.

So in our code we will add just one line of code that will dispatch DataGridEvent.ITEM_EDIT_BEGINNING and with this we will enter into the Item Editor of the newly added city just like we wanted. In add_city() function below dg_cities.selectedIndex = _id; we add the following:

dg_cities.dispatchEvent(new DataGridEvent(DataGridEvent.ITEM_EDIT_BEGINNING, false, false, 1, null, cities.length-1));

So we are dispatching a DataGridEvent, first property is the type of event that we are dispatching (hopefully I am saying this in a right way…), second and third parameters we leave as defaults, which is in both cases false, then we need to tell which coulumn of the DataGrid we want to change (id – 0, city name – 1, population – 2), so we input 1 since we want to change the city name, next parameter is again left as default – null. The last parameter we input is in which row of the column we specified we want to enter Item Editor, and that is the last row of the cities AC.

And that is all you have to do to enter Item Editor of the city name after we add one more city to DataGrid. If we wanted to edit population first we would just change parameter column from 1 to 2 and that would work as well.

private function add_city():void{
				var _id:int = cities.getItemAt(cities.length-1).id;
				_id++;
				cities.addItem({id: _id, city: 'input city', millions:0});
				dg_cities.selectedIndex = _id;
				dg_cities.dispatchEvent(new DataGridEvent(DataGridEvent.ITEM_EDIT_BEGINNING, 
										false, false, 1, null, cities.length-1));
				dg_cities.validateNow();
				dg_cities.scrollToIndex(_id);
			}
Advertisements

, , ,

  1. #1 by Nargis on February 9, 2010 - 11:32 am

    nice example
    thanks for help

  1. Using Item Editors in DataGrid, adding new data to DataGrid « Blog on engineering, programming and some more

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: