DataBinding with DataTemplate when AutoGenerateColumns="true"?

5 posts, 0 answers
  1. Simon
    Simon avatar
    23 posts
    Member since:
    Apr 2012

    Posted 21 May 2012 Link to this post

    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!
  2. Michael
    Michael avatar
    12 posts
    Member since:
    Dec 2010

    Posted 26 Oct 2012 Link to this post

    Hi.
    I also need this behaviour!!
    Can anybody please explain how this can accomplished?
    Thanks
  3. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 26 Oct 2012 Link to this post

    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.

  4. Todd
    Todd avatar
    1 posts
    Member since:
    Apr 2011

    Posted 03 Apr 2013 Link to this post

    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.
  5. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 08 Apr 2013 Link to this post

    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.

Back to Top