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


    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();
    new DataColumn("Date Column 1", typeof(DateTime)));
    new DataColumn("Date Column 2", typeof(DateTime)));
    new DataColumn("String Column 1", typeof(string)));
    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";

    radGridView1.ItemsSource = table;

    And my RadGridView currently looks like:

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

    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.

  2. Michael
    Michael avatar
    12 posts
    Member since:
    Dec 2010

    Posted 26 Oct 2012 Link to this post

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

    Posted 26 Oct 2012 Link to this post


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

    All the best,
    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}, 
                                NotifyOnValidationError=true,  ValidatesOnExceptions=true}" />
    This does not work.  I cannot specify Mode=TwoWay since I do not know the path of the DataTable dynamic reference.
  5. Nedyalko Nikolov
    Nedyalko Nikolov avatar
    871 posts

    Posted 08 Apr 2013 Link to this post


    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).

    Nedyalko Nikolov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top