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

NewRowStyle problem

6 Answers 165 Views
GridView
This is a migrated thread and some comments may be shown as answers.
danijels
Top achievements
Rank 1
danijels asked on 31 May 2010, 12:54 PM
Hi

So I have a gridview control and some buttons that should add a new row to the grid. However, each button should add a row with a different setup (some columns should be disabled/invisible).

So, my understanding is that I have to set up a few things in Grid.Resources, that being a control template with a target type and a setup and also a Style with a target type and a setter to set the temlate to the mentioned control template. First off, does this really have to be like this? Seems to me a bit too complex having to set up two different resources, one linking to another, per variation.

Anyway, I've done this like so (simplified content):
<ControlTemplate x:Key="Template1" TargetType="telerik:GridViewNewRow"
  <StackPanel Orientation="Horizontal"
     <TextBox Text="{Binding Code1}" /> 
  </StackPanel> 
</ControlTemplate> 
<ControlTemplate x:Key="Template2" TargetType="telerik:GridViewNewRow"
  <StackPanel Orientation="Horizontal"
     <TextBox Text="{Binding Code2}" /> 
  </StackPanel> 
</ControlTemplate> 
<Style x:Key="RowStyle1" TargetType="telerik:GridViewNewRow"
   <Setter Property="Template" Value="{StaticResource Template1}" /> 
</Style> 
<Style x:Key="RowStyle2" TargetType="telerik:GridViewNewRow"
   <Setter Property="Template" Value="{StaticResource Template2}" /> 
</Style> 
Then, on each buttons Click event I just add
radGrid.NewRowStyle = (Style) LayoutRoot.Resources["RowStyle1"]; 
 
(or whatever "Key" I need) before adding something to the ItemsSource observable collection. However, this doesn't seem to be enough and rows are always created the same. Is there any other way I should do it in order to create rows (and render existing ones) in different layouts?

Thank you

6 Answers, 1 is accepted

Sort by
0
danijels
Top achievements
Rank 1
answered on 02 Jun 2010, 01:14 PM
Anyone has any tips on this one, please?

Thank you
0
Kalin Milanov
Telerik team
answered on 04 Jun 2010, 08:17 AM
Hi danijels,

I have tested your case and the styles are being applied as expected. From your post it is a bit hard to identify what are you trying to accomplish. Could you please send me the real scenario you are having so I can be of assistance?

All the best,
Kalin Milanov
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.
0
danijels
Top achievements
Rank 1
answered on 04 Jun 2010, 12:57 PM
Kalin,

Thank you for your reply. I have now created a new solution and I'll paste the code here as I am not allowed to attached a zip file to the thread. The example is simple enough. It should fire up the grid, bind it to ObservableCollection<Employee> and populate the source with some data. The issue is when inserting new rows with the buttons. Each button sets NewRowStyle to its own template and then adds an object to the data source. However, while I would expect new rows to obey the styling, they look the same regardless. I would like a row to look like the pre-populated ones when clicking on "Add", while clicking on "Add predefined" should create a new row with a single dropdown control spanning over all columns.

Hope this is possible.

First, xaml:
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/datagrid"   
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"      
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    x:Class="RadControlsSilverlightApp1.MainPage"     
    xmlns:telc="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
    <Grid x:Name="LayoutRoot"
        <Grid.Resources> 
            <Style x:Key="buttonStyle" TargetType="telerik:RadButton"
                <Setter Property="ClickMode" Value="Release" /> 
            </Style> 
            <ControlTemplate x:Key="PredefinedAnswerTemplate" TargetType="telerik:GridViewNewRow"
                <StackPanel Orientation="Horizontal"
                    <ComboBox x:Name="cbRole"
                        <ComboBoxItem x:Name="Manager" x:Uid="1"></ComboBoxItem> 
                        <ComboBoxItem x:Name="Employee" x:Uid="2"></ComboBoxItem> 
                        <ComboBoxItem x:Name="Temp" x:Uid="3"></ComboBoxItem> 
                    </ComboBox> 
                </StackPanel> 
            </ControlTemplate> 
            <ControlTemplate x:Key="StandardAnswerTemplate" TargetType="telerik:GridViewNewRow"
                <StackPanel Orientation="Horizontal"
                    <TextBox Text="Predefined" /> 
                </StackPanel> 
            </ControlTemplate> 
            <Style x:Key="pdRowStyle" TargetType="telerik:GridViewNewRow"
                <Setter Property="Template" Value="{StaticResource PredefinedAnswerTemplate}" /> 
            </Style> 
            <Style x:Key="RowStyle" TargetType="telerik:GridViewNewRow"
                <Setter Property="Template" Value="{StaticResource StandardAnswerTemplate}" /> 
            </Style> 
        </Grid.Resources> 
        <telerik:RadGridView x:Name="Answers" ShowGroupPanel="False" AutoGenerateColumns="True" VerticalAlignment="Center" NewRowStyle="{StaticResource pdRowStyle}" /> 
        <StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Top"
            <telc:RadButton Style="{StaticResource buttonStyle}" Content="Add" x:Name="newanswer" Click="NewanswerClick" /> 
            <telc:RadButton Style="{StaticResource buttonStyle}" Content="Add predefined" x:Name="newpd" Click="NewpredefClick" /> 
        </StackPanel> 
    </Grid> 
</UserControl> 
 

And the .cs:
using System; 
using System.Collections.ObjectModel; 
using System.Windows; 
using System.Windows.Controls; 
 
namespace RadControlsSilverlightApp1 
    public partial class MainPage : UserControl 
    { 
        private readonly ObservableCollection<Employee> Employees; 
        public MainPage() 
        { 
            Employees = new ObservableCollection<Employee>(); 
            InitializeComponent(); 
            Answers.ItemsSource = Employees; 
            Answers.DataContext = Employees; 
            InitAnswers(); 
        } 
 
        void InitAnswers() 
        { 
            for (int i = 0; i < 10; i++) 
            { 
                Employees.Add(new Employee 
                                { 
                                    Email = "email" + i + "@email.com"
                                    FirstName = "Name" + i, 
                                    LastName = "Lastname" + i, 
                                    Phone = "332233" + i,  
                                    Role = "2" 
                                }); 
            } 
        } 
 
        private void NewanswerClick(object sender, RoutedEventArgs e) 
        { 
            Answers.NewRowStyle = (Style)LayoutRoot.Resources["RowStyle"]; 
            NewClick(); 
        } 
 
        private void NewpredefClick(object sender, RoutedEventArgs e) 
        { 
            Answers.NewRowStyle = (Style)LayoutRoot.Resources["pdRowStyle"]; 
            NewClick(); 
        } 
        private void NewClick() 
        { 
            var item = Activator.CreateInstance(typeof(Employee)); 
            Employees.Add((Employee) item); 
            Answers.ScrollIntoView(item, null); 
        } 
    } 
 
    public class Employee 
    { 
        public string FirstName { getset; } 
        public string LastName { getset; } 
        public string Phone { getset; } 
        public string Email { getset; } 
        public string Role { getset; } 
    } 
 

0
Kalin Milanov
Telerik team
answered on 10 Jun 2010, 07:33 AM
Hi Danijels,

Thank you for the code sample. By the looks of it seems there is a difference of your understanding of the "new" row and ours. The "new" row is the one which appears on top of the grid and can be used to add new data. The new row can be shown via setting the ShowInsertRow="True" (a poor naming on our part which we will fix) and is affected by the NewRowStyle and is of type GridViewNewRow. 

The ones which appear on the bottom are the same as any other row and they are affected by the RowStyle property and are of type GridViewRow.

That being said I have modified your sample (attached to this reply) to show the GridViewNewRow and the templates are being applied as expected. Please review it and let me know if this is what you are looking for.

Regards,
Kalin Milanov
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.
0
danijels
Top achievements
Rank 1
answered on 15 Jun 2010, 02:44 PM
Thank you for clearing that up, Kalin.

It was a bit poor naming, but I see the challenge. Anyway, am I to assume that what I'm looking for is not achievable, and if not, is there any indication that this might be something to look out for in the near future?

Thanks again.
0
Kalin Milanov
Telerik team
answered on 18 Jun 2010, 01:18 PM
Hi danijels,

You are right. At this point what you are looking for is not achievable. That being said we could think of a way to make the new row at the bottom more visible or provide a way for users to customize it, though I cannot provide you with any estimate on this.

I am truly sorry if this is causing you any inconveniences.

Regards,
Kalin Milanov
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
Tags
GridView
Asked by
danijels
Top achievements
Rank 1
Answers by
danijels
Top achievements
Rank 1
Kalin Milanov
Telerik team
Share this question
or