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

Gridview bind auto-generated column to custom type

2 Answers 80 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James Reategui
Top achievements
Rank 1
James Reategui asked on 09 May 2011, 06:21 AM
Hi guys,
I have a radgridview that binds to a dynamic DataTable (telerik example datatable) and the columns are of a custom type defined here:
public class WorkDisplay
{
    public bool? HomeworkDone { get; set; }
    public bool? ClassworkDone { get; set; }
}

I need the cells in the column to display as two checkboxes per column. I have tried two approaches, first was creating the following xaml DataTemplate resource and then setting the CellTemplate in the autogenerating event:
<DataTemplate x:Key="SimpleCheckBox">
        <StackPanel Orientation="Horizontal">
            <CheckBox IsChecked="{Binding Path=HomeworkDone}"/>
            <CheckBox IsChecked="{Binding Path=ClassworkDone}"/>
        </StackPanel>
</DataTemplate>

private void radGridView1_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Header.ToString() != "StudentName")
    {    
        e.Column.CellTemplate = (DataTemplate)LayoutRoot.Resources["SimpleCheckBox"];
    }
}

The result of this was the checkboxes showing up in the grid, but neither of them checked/unchecked as per the data representation.

My second approach was doing my own custom column but ended with the same result. Here the code:
public class GridViewAssignmentsColumn : GridViewDataColumn
{
    public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        var stackpanel = cell.Content as StackPanel;
 
        if (stackpanel == null)
        {
            stackpanel = new StackPanel();
            stackpanel.Orientation = Orientation.Horizontal;
            CheckBox homeworkCheckBox = new CheckBox();
            CheckBox classworkCheckBox = new CheckBox();
                 
            homeworkCheckBox.SetBinding(CheckBox.IsCheckedProperty, new Binding(this.DataMemberBinding.Path.Path.ToString())
            {
                //  Source = dataItem,
                    Mode = BindingMode.OneWay,
                    Path = new PropertyPath("HomeworkDone")
            });
            classworkCheckBox.SetBinding(CheckBox.IsCheckedProperty, new Binding(this.DataMemberBinding.Path.Path.ToString())
            {
                //  Source = dataItem,
                    Mode = BindingMode.OneWay,
                    Path = new PropertyPath("ClassworkDone")
            });
 
            stackpanel.Children.Add(homeworkCheckBox);
            stackpanel.Children.Add(classworkCheckBox);
 
            cell.Content = stackpanel;
        }
 
        return stackpanel;
    }
}

As you can see I've been reading through a number of the forums and blog postings :) but cannot come up with the precise code. I know I'm close but I'm stumped.

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 09 May 2011, 04:58 PM
Hello James Reategui,

The result of your actions that the checkBoxes are not checked corresponding to the data representation is that you are binding them wrong. You should bind them to the WorkDisplay property first(it is property of the DataContext of the current rows) and after that you should specify the specific property of WorkDisplay like:

<DataTemplate x:Key="SimpleCheckBox">
     <StackPanel Orientation="Horizontal">
          <CheckBox IsChecked="{Binding Path=WorkDisplay.HomeworkDone}"/>
          <CheckBox IsChecked="{Binding Path=WorkDisplay.ClassworkDone}"/>
    </StackPanel>
</DataTemplate>

Now you set only the CellTemplate of the cell, but if your GridView allow editing you should set CellEditTemplate as well.

If you prefer to generate the columns yourself, you could set the CellTemplate as follows:

<telerik:GridViewDataColumn  DataMemberBinding="{Binding WorkDisplay}" Header="WorkDisplay"
        CellTemplate="{StaticResource SimpleCheckBox}"
        CellEditTemplate="{StaticResource SimpleCheckBox}">
</telerik:GridViewDataColumn>

I am attaching a sample project that shows how the binding to custom type with CheckBoxes is working.

Greetings,
Didie
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
James Reategui
Top achievements
Rank 1
answered on 10 May 2011, 01:27 AM
Thanks for the help. I tried changing the Path, but had no luck. I think it would normally work, but because I am using dynamic columns, it can't resolve the path in the binding.

I ended up using a ValueConverter to take in the WorkDisplay object and bind correctly.

Thanks for the direction.
Tags
GridView
Asked by
James Reategui
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
James Reategui
Top achievements
Rank 1
Share this question
or