Sir,
I am using silverlight 2010 telerik RadGridView. I am customizing grid view and put two simple buttons for Edit and Delete for opening separate Forms for editing and deleting.
Kindly you can tell me how to mark row as selected when I am clicking on buttons unfortunately when I am clicking on button the raw was not marks as selected.
16 Answers, 1 is accepted
Yo could try the following code for your Click event handlers:
private void Button_Click(object sender, RoutedEventArgs e)
{
var senderElement = e.OriginalSource as FrameworkElement;
var row = senderElement.ParentOfType<
GridViewRow
>();
if (row != null)
row.IsSelected = true;
}
Greetings,
Milan
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
'System.Windows.FrameworkElement' does not contain a definition for 'ParentOfType' and no extension method 'ParentOfType' accepting a first argument of type 'System.Windows.FrameworkElement' could be found (are you missing a using directive or an assembly reference?
Please include the namespace Telerik.Windows.Controls to gain access to the extension method ParentOfType.
e.g.
using Telerik.Windows.Controls;
Best wishes,
Pavel Pavlov
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
<telerikControls:RadGridView x:Name="radGrid1" CurrentCellChanged="radGrid1_CurrentCellChanged" SelectionChanged="radGrid1_SelectionChanged" AutoGenerateColumns="False">
<telerikControls:RadGridView.Columns>
<telerikControls:GridViewDataColumn Header="MemberID" DataMemberBinding="{Binding MemberID}"></telerikControls:GridViewDataColumn>
<telerikControls:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}"></telerikControls:GridViewDataColumn>
<telerikControls:GridViewDataColumn HeaderTextAlignment="Center" Header="Events" DataMemberBinding="{Binding MemberID, Mode=TwoWay}" TextAlignment="Center">
<telerikControls:GridViewDataColumn.CellTemplate>
<DataTemplate>
<Button Name="btnEdit" CommandParameter="{Binding MemberID}" Content="Edit" Width="100" Height="25" Click="btnEdit_Click"></Button>
</DataTemplate>
</telerikControls:GridViewDataColumn.CellTemplate>
</telerikControls:GridViewDataColumn>
</telerikControls:RadGridView.Columns>
</telerikControls:RadGridView>
The sample project attached illustrates the approach from the previous posts in action . On clicking a button in the row , the containing row gets selected.
Kind regards,
Pavel Pavlov
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
my code bellow is as bellow. it doesn't work
<telerikGridView:RadGridView Height="100" telerik:StyleManager.Theme="Windows7" x:Name="grdAdditionalMembers" |
Background="Transparent" CanUserFreezeColumns="False" ColumnsWidthMode="Fill" |
MultipleSelect="False" AutoExpandGroups="True" ShowGroupPanel="False" AutoGenerateColumns="False" |
ItemsSource="{Binding Und.AdditionalMembers}"> |
<telerikGridView:RadGridView.Columns> |
<telerikGridView:GridViewDataColumn Header="First Name" Width="150" DataMemberBinding="{Binding FirstName}" |
IsReadOnly="True" IsGroupable="True" IsFilterable="True"/> |
<telerikGridView:GridViewDataColumn Header="Last Name" Width="150" DataMemberBinding="{Binding LastName}" |
IsReadOnly="True" IsGroupable="True" IsFilterable="True" /> |
<telerikGridView:GridViewDataColumn Header="Sex" Width="150" DataMemberBinding="{Binding Sex}" |
IsReadOnly="True" IsGroupable="True" IsFilterable="True" /> |
<telerikGridView:GridViewDataColumn Header="DOB" Width="150" DataMemberBinding="{Binding DOB}" |
IsReadOnly="True" IsGroupable="True" IsFilterable="True" /> |
<telerikGridView:GridViewColumn> |
<telerikGridView:GridViewColumn.CellTemplate> |
<DataTemplate> |
<StackPanel> |
<Button Margin="2" Height="20" Width="40" Content="edit"> |
<i:Interaction.Triggers> |
<i:EventTrigger EventName="Click"> |
<si:InvokeDataCommand Command="{Binding RaiseEdit, Mode=OneWay}"/> |
</i:EventTrigger> |
</i:Interaction.Triggers> |
</Button> |
</StackPanel> |
</DataTemplate> |
</telerikGridView:GridViewColumn.CellTemplate> |
</telerikGridView:GridViewColumn> |
</telerikGridView:RadGridView.Columns> |
</telerikGridView:RadGridView> |
Well, the DataContext for InvokeDataCommand will be a data item. the Are you sure that the data items have RaiseEdit property? Maybe you are expecting a different DataContext?
I have attached a sample project that uses InvokeDataCommand.
Kind regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
There's any way to do this with a behavior?
I'm doing this
and it says that there's no implicit conversion from GridViewRow to DependencyObject
Could you please provide us with more information about the functionality that you are trying to implement?
All the best,
Milan
the Telerik team
I used the code from commands.zip sample which worked...
((
Button)sender).ParentOfType<GridViewRow>().IsSelected = true;
I am now moving my code to a ViewModel. My command is invoked and I can pass the bound data object as a parameter but I would also like to select the row. How do I do this from a ViewModel?
Regards,
Herb
Hello Herb,
You could create SelectedItems collection on your ViewModel and you can simply add the respective items to this collection. Once this is done you will need a mechanism that synchronizes the SelectedItems of your ViewModel with the ones of the grid. Luckily we have a blog post for that.
Hope this helps.
Greetings,
Milan
the Telerik team
I had same requirement.
below code works but it calls function 2 times as there are two buttons in a row (but in different column)
public void btnEdit_Click(object sender, MouseButtonEventArgs e)
{
if (sender == null)
return;
var senderElement = e.OriginalSource as FrameworkElement;
var row = senderElement.ParentOfType<GridViewRow>();
XeRecord rec = row.Item as XeRecord;
selectStaff(rec);
}
I am not sure how the shared code calls function 2 times, would you please clarify which function do you mean? As this thread is quite old, I would suggest you to open a new support ticket under the respective product category and provide some more details on the exact case.
Regards,
Dimitrina
Telerik
See What's Next in App Development. Register for TelerikNEXT.
Hello, Dimitrina!
I have opened new thread for my issue. Please see it on
http://www.telerik.com/forums/click-event-handler-in-dynamic-data-template-in-grid-view-does-not-work-correctly
I had the same problem as Shabbir Hussain. I wanted to select row when clicking on the button that was inside the row on RadGridView. BUT I wanted also to not break the default way that grid view works. E.g. in provided code by Milan you end up with 2 rows selected if you had a row selected before clicking on the button.
For others - here is modified version that works for me:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var senderElement = e.OriginalSource as FrameworkElement;
var row = senderElement.ParentOfType<
GridViewRow
>();
if (row != null)
{
row.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = Mouse.MouseDownEvent,
Source = this,
});
}
}