Copy Paste of data along with columns names in Flex Data grid

Hi

We always find it bit difficult to attach copy functionality to a Data Grid in flex. Below example is a generic solution to attach copy functionality to a Data Grid, once you assign the data provider to the Grid. Even one can easily customize this according the needs.

Step 1:
Add an event listener on Data Grid
objectGrid.addEventListener( KeyboardEvent.KEY_UP, onKeyDown);

Step 2:
private function onKeyDown(event:KeyboardEvent) : void {
if ((event.ctrlKey || event.altKey) && (event.charCode == 67 || event.charCode == 99 ) && this.tmpGrid.selectedItems != null){
dataToCopy = getRowsData();
dataToCopy = ((headerString == ”) ? getHeaderData() : headerString)+”\n” + dataToCopy;
System.setClipboard(dataToCopy); // Copy the Grid Data
}

}

private function getRowsData():String{
var rowsData : String = ”;
for(var i:int =0;i
for(var j:int = 0; j< this.tmpGrid.columnCount; j++){
if((this.tmpGrid.columns[j] as DataGridColumn).visible)
rowsData += this.tmpGrid.selectedItems[i][(this.tmpGrid.columns[j] as DataGridColumn).dataField] +”\t”;
}
rowsData+= “\n”;
}
return rowsData;
}

// Get the Header(Column name)
private function getColumnNameData():String{
headerString = ”;
for(var j:int = 0; j< this.tmpGrid.columnCount; j++){
if((this.tmpGrid.columns[j] as DataGridColumn).visible){
headerString += (this.tmpGrid.columns[j] as DataGridColumn).headerText +”\t”;
}
}

return headerString;
}

Hope this helps.


Thanks
R Vashi

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