We need to be able to copy grids' cell/row contents to the clipboard. Am I correct in my assumption that this behavior is not supported by default? Ctrl+C on a cell/row does not do anything and I am unable to highlight the actual text in a cell in order to copy it.
We have somewhat of a workaround by using a javascript event handler for document.onkeyup/down, trapping the ctrl+c combination, and then looping through selected rows/cols/cells. Unfortunately, this breaks support for shortcut copying elsewhere on the entire page. I could probably make it so it only traps the ctrl+c combination if there are activated/selected cells/rows, but that would only fix copying elsewhere prior to selecting something. I also thought about only trapping the ctrl+c combination if the grid was focused, but I was unable to determine that (I tried attaching handlers to focus/blur to set a boolean but could not get the events to be raised).
Is there a better way to accomplish this?
Great example and fast reponse. Thanks again.
John
If you have v 9.2 or higher, you can handle the keydown event off the grid:
<ig:WebDataGrid ID="wdg1" runat="server"
DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
<Behaviors>
<ig:Selection CellClickAction="Row" RowSelectType="Single"></ig:Selection>
</Behaviors>
<ClientEvents KeyDown="keyDown" />
And here is a handler for it:
<script type="text/javascript">
function keyDown(grid, eventArgs)
{
var e = eventArgs.get_browserEvent();
if (e.ctrlKey && e.keyCode == 67)
var sel = grid.get_behaviors().get_selection();
var selRow = sel.get_selectedRows().getItem(0);
var columnCount = grid.get_columns().get_length();
var copyText = "";
for (var i = 0; i < columnCount; i++)
copyText += selRow.get_cell(i).get_text();
if (i < columnCount - 1)
copyText += "\t";
}
if (typeof (window.clipboardData) != "undefined")
window.clipboardData.setData("Text", copyText);
</script>
Placing a text into the clipboard this way only works in IE. For the other browsers it is a bit trickier.
Would it be possible to have some example code of how to copy the contents of a single cell by pressing ctrl + C? I also have row selection enabled. Here is my behavior:
<ig:Selection CellClickAction="Row" RowSelectType="Single" SelectedCellCssClass="SelectedRowItem">
</ig:Selection>
Thanks!
Ahh, yeah, that is a good idea, I should have thought of that instead of attaching it to the document.
I added two $addHandler() calls to attach the events in the InitializeGrid event for keyup/down and it works great.