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

Problems setting DisplayIndex during runtime

7 Answers 626 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Petter
Top achievements
Rank 1
Petter asked on 30 Jan 2014, 08:55 PM
Hi,

I am using the DisplayIndex property on a RadGridView to set the column order in runtime (through databinding). The problem is that I receive an ArgumentOutOfRangeException when my window is opened.

The XAML looks like the following:

<code style="color: #000;"><</code><code style="color: #069;font-weight: bold;">Telerik:GridViewDataColumn<br></code><code style="color: #000;">TextAlignment="Left"<br></code><code style="color: #000;">TextTrimming="CharacterEllipsis"<br></code><code style="color: #000;">DisplayIndex="{Binding CreatedTimestampColumnIndex}"<br></code><code><font color="#000000">IsReadOnly="True"><br></font></code><code style="color: #008200;"><!-- And more code that sets the header and the CellTemplate with binding etc... --><br></code><code style="color: #000;"></</code><code style="color: #069;font-weight: bold;">Telerik:GridViewDataColumn</code><code style="color: #000;">></code>


        public int CreatedTimestampColumnIndex<br>        {<br>            get<br>            {<br>                return createdTimestampColumnIndex;<br>            }<br> <br>            set<br>            {<br>                createdTimestampColumnIndex = value;<br>                OnPropertyChanged("CreatedTimestampColumnIndex");<br>            }<br>        }
I've verified that I do get a callback to my property methods in the ViewModel. If I set the DisplayIndex to a static value in the XAML (i.e. DisplayIndex="3") works fine.

Why doesn't this work with data binding?

Details:
.NET version: 4.5
Telerik version. 2013.Q1

Would really appreciate a solution to this, thanks!

7 Answers, 1 is accepted

Sort by
0
Petter
Top achievements
Rank 1
answered on 03 Feb 2014, 10:20 PM
Looked like my code blocks were messed up. Here's a cleaner version:

<Telerik:GridViewDataColumn
TextAlignment="Left"
TextTrimming="CharacterEllipsis"
DisplayIndex="{Binding CreatedTimestampColumnIndex}"
IsReadOnly="True">

<!-- And more code that sets the header and the CellTemplate with binding etc... -->
</Telerik:GridViewDataColumn>

and

public int CreatedTimestampColumnIndex
get
{
return createdTimestampColumnIndex;
}
set
{
createdTimestampColumnIndex = value;
OnPropertyChanged("CreatedTimestampColumnIndex");
}

0
Dimitrina
Telerik team
answered on 04 Feb 2014, 12:14 PM
Hi,

Is the value of the CreatedTimestampColumnIndex a valid DisplayIndex? I have tested it on my side and as soon as the DisplayIndex is a valid value, then it works fine. 
You say that if you set DisplayIndex="3" it works fine. Is the value of CreatedTimestampColumnIndex also 3 when it raises the exception?

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Dimitrina
Telerik team
answered on 04 Feb 2014, 12:19 PM
Hello,

I have also attached my test solution as a reference.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Petter
Top achievements
Rank 1
answered on 04 Feb 2014, 09:07 PM
Hi Didie,

Thanks for your response and the project (works for me). But I still get the OutOfRange exception when the value (which is valid) is taken through binding instead of a static value. More details:

This works:

                        <Telerik:GridViewDataColumn
                            Width="105"
                            MinWidth="105"
                            IsSortable="True"
                            HeaderTextAlignment="Center"
                            IsFilterable="False"
                            TextAlignment="Left"
                            TextTrimming="CharacterEllipsis"
                            IsReadOnly="True"
                            DataMemberBinding="{Binding uld_insert_date_time}"
                            DisplayIndex="3"
                            Header="Created">

                            <Telerik:GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock
                                        Text="{Binding uld_insert_date_time, StringFormat={}{0:dd-MMM-yyyy HH:mm}}"/>
                                </DataTemplate>
                            </Telerik:GridViewColumn.CellTemplate>

                        </Telerik:GridViewDataColumn>

This gives me the exception:

                        <Telerik:GridViewDataColumn
                            Width="105"
                            MinWidth="105"
                            IsSortable="True"
                            HeaderTextAlignment="Center"
                            IsFilterable="False"
                            TextAlignment="Left"
                            TextTrimming="CharacterEllipsis"
                            IsReadOnly="True"
                            DataMemberBinding="{Binding uld_insert_date_time}"
                            DisplayIndex="{Binding CreatedTimestampColumnIndex}"
                            Header="Created">

                            <Telerik:GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock
                                        Text="{Binding uld_insert_date_time, StringFormat={}{0:dd-MMM-yyyy HH:mm}}"/>
                                </DataTemplate>
                            </Telerik:GridViewColumn.CellTemplate>

                        </Telerik:GridViewDataColumn>

... and I have verified that the returned value from the following method:

        public int CreatedTimestampColumnIndex
        {
            get
            {
                return createdTimestampColumnIndex;
            }

            set
            {
                createdTimestampColumnIndex = value;
                OnPropertyChanged("CreatedTimestampColumnIndex");
            }
        }

is 3 (I have 17 columns).

(And yes, I realize that I used a Telerik:GridViewColumn and not Telerik:GridViewDataColumn in my original post (sorry about that), but I changed the column in your project to a Telerik:GridViewDataColumn and it worked fine).

The exception occurs when the window is opened.

The only significant difference between your and my project I can think of is the following:

- In my xaml.cs file for my view, I set the data context for the entire view to the view model (this.DataContext = ViewModel; after the call to InitializeComponent();). You set it for the grid (DataContext="{StaticResource MyViewModel}") in the xaml file.

But shouldn't really matter.

Any ideas would be most helpful, thanks!

/ Petter
0
Petter
Top achievements
Rank 1
answered on 04 Feb 2014, 09:38 PM
Oops, when I changed the sequence of the calls in the xaml.cs to the following:

            this.DataContext = ViewModel;
            InitializeComponent();

It started working.

But now, when I set the DisplayIndex value of my to columns Created and Last updated to 17 and 3 respectively, the DisplayIndex property is not called for the latter, i.e.

CreatedTimestampColumnIndex = 17;
UpdatedTimestampColumnIndex = 3;

will enforce the Created column to change column index, but not the Last updated. I simply never get a callback to

        public int UpdatedTimestampColumnIndex
        {
            get
            {
                return updatedTimestampColumnIndex;
            }

            set
            {
                updatedTimestampColumnIndex = value;
                OnPropertyChanged("UpdatedTimestampColumnIndex");
            }

despite the call to OnPropertyChanged.

Is this becuase the DisplayIndex of it already changed since I first changed the DisplayIndex of the Created column, this forcing the Last updated column to "automatically" change DisplayIndex? How do I deal with this?
0
Petter
Top achievements
Rank 1
answered on 05 Feb 2014, 12:11 AM
I got it working now.

Solution: The binding mode must explicitly be set to TwoWay (this is usually default).
0
Dimitrina
Telerik team
answered on 05 Feb 2014, 10:19 AM
Hello,

I am glad to hear you got it working fine!

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Petter
Top achievements
Rank 1
Answers by
Petter
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or