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

[Solved] RadGridView binding with indexer

1 Answer 148 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pierre Grondin
Top achievements
Rank 1
Pierre Grondin asked on 25 Feb 2010, 05:30 PM
VS2008, Vista, SL3

I am trying to create a spreadsheet-like control using the RadGridView. The path to the column data contains an indexer. The indexer seem to work given that if a double-click on a cell, the cell editor show me the right value and allow me to modify it. However the cell viewer fails to show the value so the table appread to be blank. Each cell must be editable.

Here is my code that demonstrates my attempt via usage of a DataGrid (which do not allow me to write the cell value) and a RadGridView that has the faulty behavior.

I have tried using the converter trick but it produced the same (blank) effect.

 

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="SilverlightTable.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    Width="600" Height="300"
    >
   
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <data:DataGrid Name="DataGrid" Grid.Column="0" AutoGenerateColumns="False"  IsReadOnly="False" Margin="15" />
        <Telerik:RadGridView x:Name="RadGrid" Grid.Column="1" AutoGenerateColumns="False" IsReadOnly="False" IsFilteringAllowed="False" Margin="15" />
    </Grid>
</UserControl>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using Telerik.Windows.Controls;

namespace SilverlightTable
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(OnLoaded);
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Grid 1, DataGrid.
            DataGrid.Columns.Add(CreateDataGridColumn("A", "[0].Value"));
            DataGrid.Columns.Add(CreateDataGridColumn("B", "[1].Value"));
            DataGrid.Columns.Add(CreateDataGridColumn("C", "[2].Value"));
            DataGrid.Columns.Add(CreateDataGridColumn("D", "[3].Value"));
            DataGrid.ItemsSource = GetSimpleList();

            // Grid 2, RadGrid.
            RadGrid.Columns.Add(CreateRadGridColumn(typeof(string), "[0].Value"));
            RadGrid.Columns.Add(CreateRadGridColumn(typeof(string), "[1].Value"));
            RadGrid.Columns.Add(CreateRadGridColumn(typeof(string), "[2].Value"));
            RadGrid.Columns.Add(CreateRadGridColumn(typeof(string), "[3].Value"));
            RadGrid.ItemsSource = GetSimpleList();
        }

        private static GridViewDataColumn CreateRadGridColumn(Type columnType, string dataPath)
        {
            var column = new GridViewDataColumn
                {
                     DataType = columnType
                    ,UniqueName = dataPath
                    ,Header = dataPath
                    ,IsSortable = false
                };
            return column;
        }

        private static DataGridTextColumn CreateDataGridColumn(string header, string dataPath)
        {
            var column = new DataGridTextColumn
                 {
                      Header = header
                     ,CanUserSort = false
                     ,Binding = new Binding(dataPath){ Mode = BindingMode.TwoWay }
                 };
            return column;
        }

        private static IEnumerable GetSimpleList()
        {
            return new List<IEnumerable<XCell>>
                   {
                        new List<XCell> { new XCell{Value = "1A"}, new XCell{Value = "1B"}, new XCell{Value = "1C"}, new XCell{Value = "1D"} }
                       ,new List<XCell> { new XCell{Value = "2A"}, new XCell{Value = "2B"}, new XCell{Value = "2C"}, new XCell{Value = "2D"} }
                       ,new List<XCell> { new XCell{Value = "3A"}, new XCell{Value = "3B"}, new XCell{Value = "3C"}, new XCell{Value = "3D"} }
                       ,new List<XCell> { new XCell{Value = "4A"}, new XCell{Value = "4B"}, new XCell{Value = "4C"}, new XCell{Value = "4D"} }
                   };
        }
    }

    public class XCell : INotifyPropertyChanged
    {
        private String _value;
        public String Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    _value = value;
                    RaisePropertyChanged("Value");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged == null) return;
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


What am I doing wrong?

Pierre

1 Answer, 1 is accepted

Sort by
0
Pierre Grondin
Top achievements
Rank 1
answered on 25 Feb 2010, 09:55 PM
I found my problem, apparently, RadGridView uses the indexer  differently when is looks for the data to display and when it populates its data structure.

To fix the display problem, I replaced the 'new List<IEnumerable<XCell>>' by 'new List<IList<XCell>>' and now it works.

Pierre
Tags
GridView
Asked by
Pierre Grondin
Top achievements
Rank 1
Answers by
Pierre Grondin
Top achievements
Rank 1
Share this question
or