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

RadGridView -> Moving rows up and down with right clicking

5 Answers 131 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Darren
Top achievements
Rank 1
Darren asked on 21 Feb 2019, 06:20 AM

Hello,

I trying to move rows up or down depending on user selection from the Context Menu.

And for some reason (still trying to figure out), the IndexOf is returning -1.

Below are my XAML code =>

             <telerik:RadGridView x:Name="gridView1" Grid.Row ="1" AutoGenerateColumns="False" Margin="0,5,0,10" FontSize="12" Deleted="OnDeletedIO"

                                                               Deleting="OnDeletingIO" telerik:StyleManager.Theme="Summer" DataGridCell.GotFocus="DataGrid_CellGotFocus">    
                        <telerik:RadGridView.ContextMenu>
                            <ContextMenu >
                                <MenuItem Header="MoveUp"  Click="MoveUp_click"/>
                            </ContextMenu>
                        </telerik:RadGridView.ContextMenu>
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding name}" Header="Name" IsReadOnly="False"/>
                            <telerik:GridViewComboBoxColumn x:Name="ComboGroup" DataMemberBinding="{Binding group}"  Header="Group"/>
                            <telerik:GridViewComboBoxColumn x:Name="ComboRTU" DataMemberBinding="{Binding rtu}"  Header="RTUs"/>
                            <telerik:GridViewComboBoxColumn x:Name="ComboOP" DataMemberBinding="{Binding op}"  Header="Operation"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding addr}" Header="Address"/>
                            <telerik:GridViewComboBoxColumn x:Name="ComboTypes2" DataMemberBinding="{Binding dataType}"  Header="Data Type"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding swapByte}" Header="Byte&#x0a;Swapping"
                            IsReadOnly="True">                                
                           </telerik:GridViewDataColumn>
                        </telerik:RadGridView.Columns>
                  </telerik:RadGridView>

 

C# code =>

namespace MBM_WPF
{    
    public partial class MainWindow : Window
    {

        MBM mbm1 ;
        MBM_view mbmView;        
 
        public MainWindow(string path)
        {
            StyleManager.ApplicationTheme = new SummerTheme();
            InitializeComponent();
            logger = LogManager.GetLogger("Instruct.MBM_Main");
            Configpath = path;
            
            //Populating ItemsSource here
            
            catch 
            {
                mbm1 = new MBM();
                mbmView = new MBM_view(mbm1, OnSettingChanged);
                BindMyData();
            }
        }

private void MoveUp_click(object sender, RoutedEventArgs e)
        {            
            //Get the clicked MenuItem
            var menuItem = (MenuItem)sender;
            
            //Get the ContextMenu to which the menuItem belongs
            var contextMenu = (ContextMenu)menuItem.Parent;            

            //Find the placementTarget
            var item = (RadGridView)contextMenu.PlacementTarget;
            
            int index = this.gridView1.Items.IndexOf(item.SelectedItem);

            this.gridView1.Items.Remove(item.SelectedItem);
           
            this.gridView1.Items.Insert(index - 1, item.SelectedItem);
            item.SelectedItem = item; 

            mbmView.IoListCleanUp();
            mbmView.AlarmsCleanUp();    

        }
     }
}

Please help.

Thank you!!

 

Regards,

Darren

 

5 Answers, 1 is accepted

Sort by
0
Dinko
Telerik team
answered on 25 Feb 2019, 09:35 AM
Hi Darren,

Thank you for the provided code snippet.

I would suggest working with the collection which the RadGridView is bound for. I have prepared a sample project which demonstrates this approach. The whole logic is performed in the ViewModel. 

Hope this approach will work for you.

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Darren
Top achievements
Rank 1
answered on 12 Mar 2019, 08:46 AM

Hello Dinko,

Thank you for the example given! It works! The row can now be move up and down!

However, there is a small problem on my side. I am trying to simplify the question here by removing most of the code implementation. I just includes 1 small portion of the code to illustrate my problem here.

After I added the telerik:RadContextMenu code into my original xaml =>

<telerik:RadGridView x:Name="RTUViewGrid" AutoGenerateColumns="False" Margin="10,0,10,10" FontSize="12"  IsFilteringAllowed="False" CanUserSortGroups="False" CanUserReorderColumns="False" CanUserSortColumns="False" ShowGroupPanel="False">
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu x:Name="RTUViewGridContextMenu">
                        <telerik:RadContextMenu.Items>
                            <telerik:RadMenuItem Header="Delete"/>
                        </telerik:RadContextMenu.Items>
                    </telerik:RadContextMenu>
                </telerik:RadContextMenu.ContextMenu>

</telerik:RadGridView>

 

The exe file generated (from wixproj installer) can no longer runs (the screen can no longer be loaded).

However, the screen can still be loaded while I executed it using Visual Studio Debug (thats the reason why I was able to test out the Move Up and Move Down successfully).

Is there any dll file that needs to be included while generating this Installer Package?

Thank you Dinko!

 

Best Regards,

Darren

 

 

0
Dinko
Telerik team
answered on 14 Mar 2019, 12:32 PM
Hello Darren,

I am not familiar with the wixproj installer. I need to point out that the RadContextMenu require Telerik.Windows.Controls.Navigation.dll and Telerik.Windows.Controls.dll. You can double check if these assemblies are referenced or an error will appear.

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
answered on 28 Jul 2020, 08:22 PM
A cs file missed in the project.
0
Martin Ivanov
Telerik team
answered on 31 Jul 2020, 09:00 AM

Hello David,

The RelayCommand.cs file in the Dinko's project is a leftover that is not necessary to run the application. To resolve the error, delete the file reference from the project.

Regards,
Martin Ivanov
Progress Telerik

Tags
GridView
Asked by
Darren
Top achievements
Rank 1
Answers by
Dinko
Telerik team
Darren
Top achievements
Rank 1
David
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or