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

DataBinding with DataTemplate when AutoGenerateColumns="true"?

4 Answers 296 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 21 May 2012, 06:45 PM
Hello,

I have a RadGridView that will be populated by a dynamically generated DataTable. I have therefore set the RadGridView property as follows: AutoGenerateColumns="True". My DataTable will contain dates, strings and integers. I want to be able to use a DataTemplate for each data type in the automatically generated columns of the grid. For instance, for string columns, I will want to use a TextBox with text wrapping, which accepts returns and has a certain maximum width.

For the purpose of this question, my datatable will be created and bound to the RadGridView as follows:

DataTable table = new DataTable();
table.Columns.Add(
new DataColumn("Date Column 1", typeof(DateTime)));
table.Columns.Add(
new DataColumn("Date Column 2", typeof(DateTime)));
table.Columns.Add(
new DataColumn("String Column 1", typeof(string)));
table.Columns.Add(
new DataColumn("String Column 2", typeof(string)));

DataRow row1 = table.NewRow();
row1[table.Columns[0]] = DateTime.Now;
row1[table.Columns[1]] = DateTime.Now.AddDays(10);
row1[table.Columns[2]] =
"some string";
row1[table.Columns[3]] =
"some other string";

table.Rows.Add(row1);
radGridView1.ItemsSource = table;

And my RadGridView currently looks like:

<telerik:RadGridView HorizontalAlignment="Stretch" Name="radGridView1" VerticalAlignment="Stretch" AutoGenerateColumns="True">
    <telerik:RadGridView.Resources>
        <DataTemplate x:Key="TextCellEditorTemplate">
            <TextBox Background="LightYellow" MaxWidth="250" TextWrapping="Wrap" AcceptsReturn="True"
                        Text="{Binding WHAT_GOES_HERE?}" />
        </DataTemplate>
        <DataTemplate x:Key="DateTimeCellEditorTemplate">
            ...
        </DataTemplate>
        <DataTemplate x:Key="IntegerCellEditorTemplate">
            ...
        </DataTemplate>
    </telerik:RadGridView.Resources>
</telerik:RadGridView>

Can you help me specify the binding of the text property of the DataTemplate that uses a TextBox? It should be a two-way binding with the underlying cell in the DataTable. Could you provide me with a basic working example of what I am trying to achieve? This would be greatly appreciated.

Thanks!

4 Answers, 1 is accepted

Sort by
0
BKR AG
Top achievements
Rank 1
answered on 26 Oct 2012, 08:28 AM
Hi.
I also need this behaviour!!
Can anybody please explain how this can accomplished?
Thanks
0
Vlad
Telerik team
answered on 26 Oct 2012, 08:38 AM
Hello,

 You need to handle AutoGeneratingColumn event and assign desired template to CellTemplate for desired column. 

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Todd
Top achievements
Rank 1
answered on 03 Apr 2013, 02:12 PM
I too am experiencing the same issue.  I have assigned CellTemplate in AutoGeneratingColumn event for data type DataTime.  However, I am not sure how to code the binding expression in the template.  Without it, the cell does not store the datetime selected.

Ive tried
        <DataTemplate x:Name="DateTimeColumnTemplate">
            <telerik:RadDateTimePicker SelectedValue="{Binding 
                            Converter={StaticResource NullDateConverter}, 
                            ConverterParameter='',
                            NotifyOnValidationError=true,  ValidatesOnExceptions=true}" />
        </DataTemplate>
This does not work.  I cannot specify Mode=TwoWay since I do not know the path of the DataTable dynamic reference.
0
Nedyalko Nikolov
Telerik team
answered on 08 Apr 2013, 02:31 PM
Hi,

You could create DataTemplate in code behind inside AutoGeneratingColumnEvent, at this moment you should have all the information you need (like name of the property).

Regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Simon
Top achievements
Rank 1
Answers by
BKR AG
Top achievements
Rank 1
Vlad
Telerik team
Todd
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or