WPF Telerik RadGridView: databinding not working with datarow derived class

380 views Asked by At

I am attempting to databind using a type derived from System.Data.DataRow which has a public property that returns calculated data which is not a column in my DataTable.

I can sucessfully databind my GridViewDataColumn to a property(TestString) that exists in my datatable, but I cannot databind to my property(TestHashID) that returns data not in the datatable.

Sample Included:

XAML
<Window x:Class="Junk.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"
            Title="MainWindow" Height="350" Width="525">
    <Grid>
    <telerik:RadGridView x:Name="cGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Height="261" Width="445" Margin="39,25,0,0" />
</Grid>

Code Behind
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;

namespace Junk
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        TestTable mTestTable = new TestTable();

        public MainWindow()
        {
            InitializeComponent();
            CreateData();
            CreateGrid();
        }

        void CreateData()
        {
            Test sRow;
            sRow = (Test)mTestTable.NewRow();
            sRow.TestString = "123";
            mTestTable.Rows.Add(sRow);

            sRow = (Test)mTestTable.NewRow();
            sRow.TestString = "456";
            mTestTable.Rows.Add(sRow);

            sRow = (Test)mTestTable.NewRow();
            sRow.TestString = "789";
            mTestTable.Rows.Add(sRow);
        }

        void CreateGrid()
        {
            cGrid.AutoGenerateColumns = false;

            var sCol1 = new GridViewDataColumn();
            sCol1.Header = "Test_String";
            sCol1.DataMemberBinding = new Binding("TestString");
            cGrid.Columns.Add(sCol1);

            var sCol2 = new GridViewDataColumn();
            sCol2.Header = "Test_HashID";
            sCol2.DataMemberBinding = new Binding("TestHashID");
            cGrid.Columns.Add(sCol2);

            cGrid.ItemsSource = mTestTable;
        }
    }

    ///////////////////////////////////////////////////////////////////
    // class TestTable
    ///////////////////////////////////////////////////////////////////
    [Serializable]
    public class TestTable : DataTable
    {
        public DataColumn TestStringColumn { get { return this.Columns["TestString"]; } }

        public TestTable() : base("Test")
        {
            DataColumn sDataColumn = new DataColumn("TestString", typeof(string), "", System.Data.MappingType.Element);
            sDataColumn.AllowDBNull = false;
            sDataColumn.DefaultValue = "";
            this.Columns.Add(sDataColumn);
        }

        protected override DataRow NewRowFromBuilder(DataRowBuilder builder) { return new Test(builder); }
        protected override System.Type GetRowType() { return typeof(Test); }

    } // End TestTable

    //////////////////////////////////////////////////////////////////////
    // class Test
    //////////////////////////////////////////////////////////////////////
    public partial class Test : DataRow
    {
        public TestTable TestTable { get { return (TestTable)this.Table; } }

        public Test(DataRowBuilder rb)
            : base(rb)
        {
        }

        public string TestString
        {
            get
            {
                return this[TestTable.TestStringColumn].ToString().TrimEnd();
            }
            set
            {
                if (TestString != value.TrimEnd()) this[TestTable.TestStringColumn] = value.TrimEnd();
            }
        }

        public int TestHashID
        {
            get { return TestString.GetHashCode(); }
            set { ;}
        }

    }
}

Here is the result(I cannot post screenshot) :

When I run this sample, the Test_String column is populated with data as expected, but the Test_HashID column is blank.

Any help would be much appreciated!

0

There are 0 answers