Add a row number to the flex 2 DataGrid

Source: Internet
Author: User

Http://www.cflex.net/showfiledetails.cfm? Channelid = 1 & object = file & objectid = 735

Sometimes, we need to display the row number of the current row in the DataGrid control. If the database Source of the DataGrid (dataprovider) is a collection, the problem will be handled, you only need to add a virtual computing column to the DataGrid and customize the labelfunction of the column, in this function, call getitemindex to calculate the index of the current row and Add 1 to it and then return it as a character. Note: These row numbers are dynamically generated and always in a certain order, and it has no relationship with the sorting of other columns. If you want the row number to change with the change of the row (usually when sorting a column ), then you need to re-allocate the row number for all rows, which must be controlled by a program in dataprovider. The sample code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"
Creationcomplete = "initapp ()">
<Mx: SCRIPT> <! [CDATA [
Import MX. Collections. arraycollection;
Import MX. Controls. Alert;

[Bindable] private VaR _ ACDP: arraycollection;
 
Public Function initapp (): void
{
VaR oitem: object;
_ ACDP = new arraycollection ();
Oitem = {album: "the lost chord", Artist: "The Moody Blues", price: "15.99 "};
_ ACDP. additem (oitem );
Oitem = {album: "Meddle", Artist: "Pink Floyd", price: "17.99 "};
_ ACDP. additem (oitem );
Oitem = {album: "Trespass", Artist: "Genesis", price: "18.99 "};
_ ACDP. additem (oitem );
 
} // Initapp

Private function lfrownum (oitem: object, icol: INT): String
{
VaR iindex: Int = _ ACDP. getitemindex (oitem) + 1;
Return string (iindex );
}
 
]> </MX: SCRIPT>

<Mx: vbox>
<Mx: DataGrid id = "dgsource" dataprovider = "{_ ACDP }"
Editable = "true">
<Mx: columns>
<Mx: array>
<Mx: datagridcolumn headertext = "Row #" labelfunction = "lfrownum"/>
<Mx: datagridcolumn headertext = "album" datafield = "album"/>
<Mx: datagridcolumn headertext = "artist" datafield = "artist"/>
<Mx: datagridcolumn headertext = "price" datafield = "price"/>
</MX: array>
</MX: columns>
</MX: DataGrid>
</MX: vbox>
</MX: Application>

 

Null

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.