This is a migrated thread and some comments may be shown as answers.

ins/delete/select all/copy/paste from code behind

4 Answers 174 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 1
Jim asked on 25 Aug 2011, 10:06 AM
Hello
I can ins/delete/select all/copy/paste with keys Insert/Delete/^A/^C/^V
This all seems to work OK.

I have a nice right click context menu on the grid.

Is it possible to invoke the ins/delete/select all/copy/paste from code behind.
Without having to manually do all the selects or read/write the clipboard directly.
So for example the copy menu item would somehow invoke something that simulates pressing ^C ^V etc
Thanks
Jim

4 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 29 Aug 2011, 12:42 PM
Hello Jim,

Yes, all this is possible by using RadGridView's API. Please consider the following help articles:

Adding and inserting rows
Selecting rows and cells programmaticaly
* Please note that you can select/deselect all rows by calling SelectAll/ClearSelection methods respectively.

You can use the following code to copy and paste between grid cells with code:
RadDropDownMenu menu = this.radGridView1.TableElement.VisualRows[3].VisualCells[2].MergeMenus(this.radGridView1.ContextMenuManager, null);
string copyItemText = RadGridLocalizationProvider.CurrentProvider.GetLocalizedString(RadGridStringId.CopyMenuItem);
string pasteItemText = RadGridLocalizationProvider.CurrentProvider.GetLocalizedString(RadGridStringId.PasteMenuItem);
RadMenuItem copyMenuItem = FindMenuItem(menu, copyItemText);
RadMenuItem pasteMenuItem = FindMenuItem(menu, pasteItemText);
copyMenuItem.PerformClick();
//
pasteMenuItem.PerformClick();
 
Best wishes,
Jack
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Jim
Top achievements
Rank 1
answered on 05 Sep 2011, 03:30 PM
Not quite sure I follow you ..

I have a radgridview with a content menu
And code behind when user right clicks and selects copy is

Private Sub PricesRadContextMenu_ItemClick(sender As System.Object, e As Telerik.Windows.RadRoutedEventArgs) Handles PricesRadContextMenu.ItemClick
    ' Get the clicked context menu item
    Dim menuItem As RadMenuItem = TryCast(DirectCast(e, RadRoutedEventArgs).OriginalSource, RadMenuItem)
    Select Case menuItem.Header.ToString
        Case "Copy"

So I need the code to do just what pressing control C would do.
I don't want top select things etc in code behind, just do what control C does.
Hope that makes sense ?

Thanks
Jim



0
Jim
Top achievements
Rank 1
answered on 06 Sep 2011, 08:04 AM
Hah I think I have seen the light
xaml
<telerik:RadContextMenu.ContextMenu>
    <telerik:RadContextMenu Name="PricesRadContextMenu">
        <telerik:RadMenuItem Header="Copy" Name="CopyRadMenuItem" Command="telerik:RadGridViewCommands.Copy" CommandParameter="{Binding SelectedItem}" IsEnabled="True"/>
        <telerik:RadMenuItem Header="Paste" Name="PasteRadMenuItem" Command="telerik:RadGridViewCommands.Paste" CommandParameter="{Binding SelectedItem}" IsEnabled="True"/>
        <telerik:RadMenuItem Header="Delete" Name="DeleteRadMenuItem" Command="telerik:RadGridViewCommands.Delete" CommandParameter="{Binding SelectedItem}" IsEnabled="True"/>
        <telerik:RadMenuItem IsSeparator="True" />
        <telerik:RadMenuItem Header="Select All" Name="SelectAllRadMenuItem" IsEnabled="True"/>
        <telerik:RadMenuItem Header="Clear Select" Name="ClearSelectRadMenuItem" IsEnabled="True"/>
    </telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
VB code
Private Sub PricesRadContextMenu_ItemClick(sender As System.Object, e As Telerik.Windows.RadRoutedEventArgs) Handles PricesRadContextMenu.ItemClick
     'Get the clicked context menu item
     Dim menuItem As RadMenuItem = TryCast(DirectCast(e, RadRoutedEventArgs).OriginalSource, RadMenuItem)
     Select Case menuItem.Header.ToString
         Case "Select All"
             PricesRadGridView.SelectAll()
             Dim x As Integer = 0
             Exit Select
         Case "Clear Select"
             PricesRadGridView.SelectedItem = Nothing
             Dim x As Integer = 0
             Exit Select
     End Select
 End Sub

However some pastes cause an exception. If I have copied from notepad and not selected the final CR the I get the following.
Any way I can catch or avoid this ?

Microsoft JScript runtime error: Unhandled Error in Silverlight Application 
Code: 4004    
Category: ManagedRuntimeError       
Message: System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.GridView.Clipboard.GridViewCellInfoExtensions.SetPastedValue(GridViewCellInfo cell, String value)

Thanks
Jim
0
Konstantina
Telerik team
answered on 07 Sep 2011, 01:15 PM
Hello Jim,

You are copying and pasting from notepad? Is that correct? The GridViewCommands work only with selected elements from GridView. If you need to copy and paste other objects, please consider creating a custom command for this and setting it to the ContextMenu. For more info please see this help article.
For more information about GridView's Commands please refer to this demo.

All the best,
Konstantina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Jim
Top achievements
Rank 1
Answers by
Jack
Telerik team
Jim
Top achievements
Rank 1
Konstantina
Telerik team
Share this question
or