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

RadGridViewCommands

2 Answers 138 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 1
Brett asked on 13 Apr 2013, 01:18 AM
I cannot get the insert, delete, commit, or cancel commands to work using a ViewModel.  I have followed the online example here:

http://demos.telerik.com/silverlight/#GridView/Commands

I click on any of the buttons and nothing happens.  Could someone please review my code and see what I am missing?

Here is my code:

<navigation:Page xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="BRP.SL.Views.Admin.Companies"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:domain="clr-namespace:BRP.SL.Web.Services"

mc:Ignorable="d"

xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"

xmlns:vm="clr-namespace:BRP.SL.ViewModels"

d:DesignWidth="640" d:DesignHeight="480"

Title="Companies">

<navigation:Page.Resources>

<vm:CompaniesViewModel x:Key="CompaniesVM"/>

</navigation:Page.Resources>

<Grid x:Name="LayoutRoot" Margin="10" DataContext="{StaticResource CompaniesVM}">

<Grid.RowDefinitions>

<RowDefinition Height="40" />

<RowDefinition Height="50" />

<RowDefinition Height="*" />

<RowDefinition Height="60" />

</Grid.RowDefinitions>

<TextBlock

Text="Companies"

Style="{StaticResource HeaderTextStyle}"

Grid.Row="0"/>

<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">

<telerik:RadButton Style="{StaticResource AddButtonMetroStyle}"

Margin="5"

ToolTipService.ToolTip="Add"

Command="telerikGrid:RadGridViewCommands.BeginInsert"

CommandTarget="{Binding ElementName=RadGridView1}"/>

<telerik:RadButton Style="{StaticResource DeleteButtonMetroStyle}"

Margin="5"

ToolTipService.ToolTip="Delete"

Command="telerikGrid:RadGridViewCommands.Delete"

CommandTarget="{Binding ElementName=RadGridView1}" />

<telerik:RadButton Style="{StaticResource SaveButtonMetroStyle}"

Margin="5"

ToolTipService.ToolTip="Save"

Command="telerikGrid:RadGridViewCommands.CommitEdit"

CommandTarget="{Binding ElementName=RadGridView1}"/>

<telerik:RadButton Style="{StaticResource CancelButtonMetroStyle}"

Margin="5"

ToolTipService.ToolTip="Cancel"

Command="telerikGrid:RadGridViewCommands.CancelRowEdit"

CommandTarget="{Binding ElementName=RadGridView1}"/>

</StackPanel>

<telerik:RadGridView x:Name="RadGridView1" GroupRenderMode="Flat"

ItemsSource="{Binding Companies}"

SelectionMode="Extended"

AutoGenerateColumns="False"

Margin="0,5,0,0"

Grid.Row="2"

ActionOnLostFocus="None"

CanUserDeleteRows="True"

CanUserFreezeColumns="False"

RowIndicatorVisibility="Collapsed"

IsReadOnly="False">

<telerik:RadGridView.Columns>

<telerik:GridViewColumn Width="90">

<telerik:GridViewColumn.CellTemplate>

<DataTemplate>

<telerik:RadButton Content="Delete"

Command="telerikGrid:RadGridViewCommands.Delete"

CommandParameter="{Binding}" />

</DataTemplate>

</telerik:GridViewColumn.CellTemplate>

</telerik:GridViewColumn>

<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"

Header="Name"

Width="220" />

<telerik:GridViewCheckBoxColumn DataMemberBinding="{Binding IsActive}"

Header="IsActive"

Width="200" />

</telerik:RadGridView.Columns>

</telerik:RadGridView>

<telerik:RadDataPager x:Name="RadDataPager1" Grid.Row="3" PageSize="15" Source="{Binding Items, ElementName=RadGridView1}" />

</Grid>

</navigation:Page>


Here is the code behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Navigation;

using Telerik.Windows.Controls;

using BRP.SL.Web.Models;

namespace BRP.SL.Views.Admin

{

public partial class Companies : Page

{

public Companies()

{

System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(RadGridViewCommands).TypeHandle);

InitializeComponent();

}

protected override void OnNavigatedTo(NavigationEventArgs e)

{

}

}

}


Here is the ViewModel:

using BRP.SL.Web.Models;

using BRP.SL.Web.Services;

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Net;

using System.ServiceModel.DomainServices.Client;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Telerik.Windows.Controls;

namespace BRP.SL.ViewModels

{

public class CompaniesViewModel : ViewModelBase

{

BRPDomainContext ctx;

private EntityQuery<Company> companyQuery;

private bool isBusy;

public bool IsBusy

{

get

{

return isBusy;

}

set

{

if (isBusy != value)

{

isBusy = value;

OnPropertyChanged(() => this.isBusy);

}

}

}

private IEnumerable<Company> _companies;

public IEnumerable<Company> Companies

{

get

{

return _companies;

}

set

{

if (_companies != value)

{

_companies = value;

OnPropertyChanged("Companies");

}

}

}

public void LoadCompanies()

{

isBusy = true;

ctx.Companies.Clear();

companyQuery = ctx.GetCompaniesQuery().OrderBy(x => x.Name);

ctx.Load(companyQuery, LoadCompaniesCompleted, null);

}

private void LoadCompaniesCompleted(LoadOperation<Company> op)

{

IsBusy = false;

Companies = op.Entities;

}

public CompaniesViewModel()

{

if (DesignerProperties.IsInDesignTool)

{

var list = new List<Company>

{

new Company{Name="test1", IsActive = true},

new Company {Name="test2", IsActive = false},

new Company{Name="test1", IsActive = true},

new Company {Name="test2", IsActive = false},

new Company{Name="test1", IsActive = true},

new Company {Name="test2", IsActive = false}

};

_companies = list;

}

else

{

ctx = new BRPDomainContext();

LoadCompanies();

}

}

}

}



Here is
Any help would be greatly appreciated.

Thanks,
Brett

2 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 15 Apr 2013, 06:22 AM
Hello Brett,

The commands will work only if the source collection support Add/ Remove actions. Another condition in order for BeginInsert command to work is that your business object has a default constructor. Check out this article form more information.

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Brett
Top achievements
Rank 1
answered on 15 Apr 2013, 09:17 PM
I used the Load Operation to fill an ObservableCollection and the commands work.  Thank you for your help!
Tags
GridView
Asked by
Brett
Top achievements
Rank 1
Answers by
Maya
Telerik team
Brett
Top achievements
Rank 1
Share this question
or