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

Bind to column object, not column name.

1 Answer 168 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 18 Jul 2014, 02:54 PM
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?

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 23 Jul 2014, 01:06 PM
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.
 
Tags
GridView
Asked by
Scott
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or