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

Setting IsReadOnlyBinding breaks DataMemberBinding to Datatable

4 Answers 104 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lorentz
Top achievements
Rank 1
Lorentz asked on 12 Jun 2015, 02:44 PM

I have stumbled upon an issue where I only want the user to be able to edit the lower half of a matrix (It is a matrix that is symmetrical across the diagonal). I am creating a datatable based on a 2d array. 

 the grid is defined like this:

<telerik:RadGridView x:Name="matrixView" ItemsSource="{Binding MatrixTable}"
                             CanUserDeleteRows="False" CanUserInsertRows="False"
                             CanUserReorderColumns="False" CanUserSortColumns="False"
                             ShowGroupPanel="False"
                             GridLinesVisibility="Both"
                             SelectionUnit="Cell"
                             Width="AUto" Height="Auto">
</telerik:RadGridView>

And then I have the following viewmodel:

 

public class MatrixViewModel
    {
        public DataTable MatrixTable { get; set; }
 
        public MatrixViewModel(double[,] matrix)
        {
            this.MatrixTable = BuildTable(matrix);
        }
 
        private DataTable BuildTable(double[,] matrix)
        {
            var table = new DataTable();
 
            table.Columns.Add("Name", typeof(string));
            for (int col = 0; col < matrix.GetLength(0); col++)
            {
                table.Columns.Add(col.ToString(), typeof(double));
            }
 
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                var row = table.NewRow();
                row[0] = i.ToString();
 
                for (int j = 0; j <= i; j++)
                {
                    row[j + 1] = matrix[i, j];
                }
                table.Rows.Add(row);
            }
            return table;
        }
    }

NullvalueToBoolean is defined as follows:

[ValueConversion(typeof(object), typeof(Boolean))]
    public class NullValueToBoolean : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value == null;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

And finally the codebehind:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MatrixViewModel(new double[,]{
                {1,0,0},
                {0.25,1,0},
                {0.25,0.75,1}
            });
            this.matrixView.AutoGeneratingColumn += matrixView_AutoGeneratingColumn;
        }
 
        void matrixView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            var column = (GridViewDataColumn)e.Column;
            column.IsFilterable = false;
 
            if (column.DataType == typeof(double?))
            {
                column.IsReadOnlyBinding = new Binding(column.DataMemberBinding.Path.Path)
                {
                    Converter = new NullValueToBoolean()
                };
            }
            else
            {
                column.IsReadOnly = true;
            }
        }
    }

 The code produces the following output when I try to edit one of the cells that are not readonly:
System.Windows.Data Error: 40 : BindingExpression path error: '0' property not found on 'object' ''DataRow' (HashCode=9659819)'. BindingExpression:Path=0; DataItem='DataRow' (HashCode=9659819); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

However, if I remove the code that sets the ReadOnlyBinding, editing works fine. Do you have a suggestion on how to fix/avoid this issue? Maybe there is a better way to solve this problem with the Telerik UI for WPF?

4 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 17 Jun 2015, 08:58 AM
Hi Lorentz,

Thank you for the code provided.

However, with it I am not able to reproduce such error. Can you please share some information on which assemblies version you are using?

Best Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Lorentz
Top achievements
Rank 1
answered on 17 Jun 2015, 09:30 AM

Certainly, the telerik controls are version 2015.1.401.45  It is built for .net 4.5

a full list of loaded assemblies can be found at http://pastebin.com/Sn5ds8h9

sysinfo for the machine on which the error occurs: http://pastebin.com/tP5cDNi2

The entire solution in which the error occurs can be found here: https://onedrive.live.com/redir?resid=8237EF9963C1295C!60134&authkey=!AJ-fHXeA6QUSPtg&ithint=file%2czip

0
Lorentz
Top achievements
Rank 1
answered on 17 Jun 2015, 09:33 AM

I accidentally swapped the link url and text attributes in the previous post, and found no way to edit it after it was posted. the correct link for the solution file is 

https://onedrive.live.com/redir?resid=8237EF9963C1295C!60134&authkey=!AJ-fHXeA6QUSPtg&ithint=file%2czip

0
Stefan
Telerik team
answered on 17 Jun 2015, 03:12 PM
Hi Lorentz,

I am not able to open the referred solution either because you have removed it, or due to the fact that I do not have permissions for that. Would it be possible for you to send the solution in a new support thread, as this would make further communication and modifications of the project easier?

Best Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Lorentz
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Lorentz
Top achievements
Rank 1
Share this question
or