Bind to column object, not column name.

2 posts, 0 answers
  1. Scott
    Scott avatar
    6 posts
    Member since:
    Jan 2014

    Posted 18 Jul 2014 Link to this post

    What is the correct way use a "DataColumn" object to tell the RadGridView which column to bind to? I can get it to bind using the "ColumnName" property on the column, but I would rather bind on the DataColumn object itself.

    Here is a test program to demonstrate the problem

    <Window x:Class="RadGridViewTest.MainWindow"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                    xmlns:radGridViewTest="clr-namespace:RadGridViewTest"
                    Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <radGridViewTest:ContextData x:Key="ContextData"/>
        </Window.Resources>
         
        <Grid DataContext="{StaticResource ContextData}">
            <telerik:RadGridView ItemsSource="{Binding Path=ExampleTable}"
                                 AutoGenerateColumns="True"
                                 ShowGroupPanel="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding ExampleColumn}" Header="Via property"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Example}" Header="Via Column Name"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </Grid>
    </Window>

    using System.Data;
    using System.Windows;
     
    namespace RadGridViewTest
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
        }
     
        public class ContextData
        {
            public ContextData()
            {
                ExampleTable = new StrongNamedDataTable();
     
                var row = ExampleTable.NewRow();
                row[ExampleTable.ExampleColumn] = "Test Data";
                ExampleTable.Rows.Add(row);
            }
     
            public StrongNamedDataTable ExampleTable { get; set; }
        }
     
        public class StrongNamedDataTable : DataTable
        {
            public StrongNamedDataTable()
            {
                ExampleColumn = this.Columns.Add("Example");
            }
     
            public DataColumn ExampleColumn { get; private set; }
        }
    }


    The column that I set via the property did not work, but the one referencing the column name and the auto generated column both worked. What is the correct way to reference a column using the property?
  2. Yoan
    Admin
    Yoan avatar
    1213 posts

    Posted 23 Jul 2014 Link to this post

    Hi Scott,

    Generally, if you want to bind GridViewDataColumn to a specific column of your DataTable, you need to use the name of DataTable's column. Please note that you will get the same behaviour with MS DataGrid.

    Regards,
    Yoan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top