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

Adding Telerik GridViewColumns via MVVM

2 Answers 68 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Keerti
Top achievements
Rank 1
Keerti asked on 06 Apr 2011, 09:52 PM
Hi,

Below is a nested itemscontrol snippet.
My problem is in the child itemscontrol i have a telerik radgridview to which i am attaching a datatable(the implementation provided by Vlad) my datatable might contain any number of columns based on my data coming from an X datasource. My question is it possible to hook up the gridviewdatacolumns to the telerik radgridview based on my datatable's columns via MVVM or any other way ? This specific implementation is needed because i need the pager control attached to this nested itemscontrol restricting the number of radgrids for each employee name.

In the screenshot AA0 , AA1 , AA2 are employee names and each have three sets of data(three radgrids) screenshot shows only 2 radgris because pagesize is 2. I have attached a screenshot when a paging is clicked as well.

Any help would be greatly appreciated.

  <ItemsControl Grid.Row="1" ItemsSource="{Binding EmpList}" Grid.Column="0">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition  Height="2*"/>
                            <RowDefinition  Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="3*" />
                            <ColumnDefinition Width="2*" />
                            <ColumnDefinition Width="4*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Path=EmployeeName}"
                                                       HorizontalAlignment="Left"                                                       
                                                       Grid.Row="0"
                                                       Margin="3"
                                                       Grid.Column="0">
                        </TextBlock>
                        <sdk:DataPager  Name="dataPager1"
                                        PageSize="2" 
                                        Grid.Row="0"
                                        Grid.Column="2"
                                        DisplayMode="FirstLastPreviousNextNumeric"
                                        NumericButtonCount="2"                                        
                                        Source="{Binding Path=PageView}"
                                        Visibility="{Binding Path=IsDataPagerVisible}"/>
                        <ItemsControl ItemsSource="{Binding Path=PageView}"
                                                          Grid.Row="1"                                                          
                                                          Grid.Column="0"
                                                          Grid.ColumnSpan="2">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <telerik:RadGridView ItemsSource="{Binding Path=EmpDetailGrid}"
                                                             AutoGenerateColumns="False"
                                                             RowIndicatorVisibility="Collapsed"
                                                             BorderThickness="0"
                                                             ShowGroupPanel="False"
                                                             IsReadOnly="True">
                                            
                                        </telerik:RadGridView>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>        

Thanks & Regards,
Keerti Somasundaram





2 Answers, 1 is accepted

Sort by
0
Keerti
Top achievements
Rank 1
answered on 06 Apr 2011, 10:08 PM
Hi,

I set the AutoGenerateColumns to True and it solved the columns not getting generated :-)
But now i need to have a column that can hold three types of controls : check box , combo box and text block..
Attached the latest screenshot.

Any ideas?

Thanks & Regards,
Keerti Somasundaram

0
Keerti
Top achievements
Rank 1
answered on 06 Apr 2011, 10:59 PM
Hi,

I achieved by using the Loaded method of the Telerik radgridview and inside this method i am adding a custom gridview column which inherits from GridViewDataColumn.

public class GridViewExtendedColumn : GridViewDataColumn
{
/// <summary>
/// generate celle element based on the datatype.
/// </summary>
/// <param name="cell"></param>
/// <param name="dataItem"></param>
/// <returns></returns>
public override System.Windows.FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
{
if (dataItem != null)
{
object data = cell.Value;
if (data is string)
{
TextBlock textBlock = new TextBlock();
textBlock.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding(cell.DataColumn.DataMemberBinding.Path.Path));
return textBlock;
}
else if (data is bool)
{
CheckBox chkBox = new CheckBox();
chkBox.HorizontalAlignment = HorizontalAlignment.Center;
chkBox.IsEnabled = false;
chkBox.SetBinding(CheckBox.IsCheckedProperty, new System.Windows.Data.Binding(cell.DataColumn.DataMemberBinding.Path.Path));
return chkBox;
}
else if (data is List<string>)
{
ComboBox cbBox = new ComboBox();
cbBox.SetBinding(ComboBox.ItemsSourceProperty, new System.Windows.Data.Binding(cell.DataColumn.DataMemberBinding.Path.Path));
return cbBox;
}
else
{
TextBlock textBlock = new TextBlock();
textBlock.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding(cell.DataColumn.DataMemberBinding.Path.Path));
return textBlock;
}
}
else
{
return null;
}
}
}

Thanks & Regards,
Keerti Somasundaram
Tags
GridView
Asked by
Keerti
Top achievements
Rank 1
Answers by
Keerti
Top achievements
Rank 1
Share this question
or