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

GridViewComboBoxColumn shows empty cells if itemsource changes

9 Answers 344 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Franziska
Top achievements
Rank 1
Franziska asked on 12 Oct 2011, 08:09 AM
Hi!

Following scenario:
I have a grid with a GridViewComboBoxColumn. The ItemsSource is a list of items, which shall only be selectable once. Therefore I am adapting the list of items, according to the edited row and hide all already selected items dynamically. This means, after all items have been selected, the list of items will be empty.

Now comes the problem:
When I enter edit mode in a row, all other rows in the column become emtpy. The grid just does not show the right thing. I guess this behavior results from the fact, that the list of items does not contain the actual items anymore...

I could work around this problem, by defining a CellTemplate, which explicitly binds to the Name of the item .

<telerik:GridViewComboBoxColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Name}" />
    </DataTemplate>
</telerik:GridViewComboBoxColumn.CellTemplate>


I have created this thread to understand WHY... and to ask if there is a better why to achieve a dynamic ItemsSource list.

Thanks and regards,

Franziska


9 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 17 Oct 2011, 08:58 AM
Hi Franziska,

Indeed the combo column will show only items that have matching value and item form the combo column ItemsSource. In case such match is not possible ( e.g. item removed)  an empty cell will be shown.

Your workaround  is a good approach. I may also suggest an alternative - instead of hiding the items already "taken"  form the ItemsSource, you may just disable them. This way once the user has taken an item , it will still show up in the combo , be present in the ItemsSource thus not breaking the logic. Yet it will be disabled and the user will not be able to select it twice.

In case you decide to go this way - let me know so I can assist you further .

Best wishes,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Franziska
Top achievements
Rank 1
answered on 24 Oct 2011, 01:12 PM
Dear Pavel,

Can you provide any code examples?

I would prefer your approach, which saves me from changing the ItemSource all the time.

Another way I thought about was applying a filter and show only items, which havn't been selected already.

Thanks,
Franziska
0
Pavel Pavlov
Telerik team
answered on 27 Oct 2011, 12:47 PM
Hello Franziska,

I am attaching the requested sample.
I has a combo column with available seats to chose form . Once a seat is chosen - it becomes disabled and not available  for selection .

Hope this helps.

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Franziska
Top achievements
Rank 1
answered on 27 Oct 2011, 01:58 PM
Dear Pavel,

Thanks for your example project.

Your solution unfortunately does not solve my problem.
First of all, I do not want to introduce an extra property on my business objects.
Second, this only works if I use one item source list for each grid. If I want to show the same choice list in different grids, I would need one instance for each grid.

Is there an easy way to hide the ComboBoxItems or to filter them?

Thanks and regards,
Franziska
0
Pavel Pavlov
Telerik team
answered on 01 Nov 2011, 02:29 PM
Hello Franziska,

I will gladly adjust the sample , however we may need some clarifications here.

"First of all, I do not want to introduce an extra property on my business objects."
I am not sure there is another way to track the taken/available items.

"Second, this only works if I use one item source list for each grid. If"
I believe the items source may be shared between several Gridviews e.g. it may be implemented as a singleton.

"Is there an easy way to hide the ComboBoxItems or to filter them?"
Yes I believe there is such way, but it would depend on point 1 ( how do we track the available/taken items) .

Best wishes,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Franziska
Top achievements
Rank 1
answered on 02 Nov 2011, 02:09 PM
Dear Pavel,

Just to clarify things:
I have a combobox with items from an enum and I want to hide those items, which have already been selected in other rows within the same GridView.

Therefore I could create a list of already selected items from the grid ItemSource, by checking each item for the corresponding property (which is bound to the DataMemberBinding of the combobox) and use this list to hide those items. 
Can you give me an example how to "filter" those items, so I dont have to change the actual ItemSource of the combobox?


Thanks again,
Franziska



0
Pavel Pavlov
Telerik team
answered on 07 Nov 2011, 01:53 PM
Hello Franziska,

I have modified the sample application.

1. I have removed the extra "IsTaken" property.
2. I have added two collections - one to hold all seats and one to hold the available seats only.
3. For the purposes of illustration I have hardcoded the AvailableSeats collection to return only the first two items. You will need to implement the logic so that the AvailableSeats contains the "filtered" items.

So as a result - the combo editor will show the "filtered" AvailableSeats , and the cell itself will behave properly having the AllSeats as ItemsSource.

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
silverkali
Top achievements
Rank 1
answered on 30 Jul 2012, 05:25 PM
Hello Franziska,

This occurs as the itemssource is changed and it no longer has the selected item from the combobox. I had the same exact scenario where i had to display values in an itemssource which are based on a value from the previous cell inside the rad grid. if any 2 rows had same value then the itemssource collection would be the same, and thus they would be displayed. Your workaround is pretty simple but solved my problem. Anyway, let us know if you have any other alternatives telerik.  
0
Burim
Top achievements
Rank 1
answered on 25 Sep 2012, 01:36 PM

The Binding engine compares the reference, since teh reference is not the same than you have to override the Equals method on your Entity and compare the object manually.

Bellow is the code that you should put with the rest of the code on your entity:

        public override bool Equals(object obj)
        {
            bool result = false;

            if (obj is Item)
            {
                Item i = (Item)obj;
                if (i.ID == ID)
                    result = true;
            }

            return result;
        }



This should do the trick!!!


From Kosovo with Love :)
Tags
GridView
Asked by
Franziska
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Franziska
Top achievements
Rank 1
silverkali
Top achievements
Rank 1
Burim
Top achievements
Rank 1
Share this question
or