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

GridViewComboBoxColumn Binding Issue

8 Answers 293 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott Michetti
Top achievements
Rank 1
Iron
Scott Michetti asked on 30 Jul 2012, 02:37 PM
Hello, I'm binding the radGridView to an observablecollection of objects which contain 3 properties. So the grid is displayed with 3 columns. 1 of the columns is an observablecollection which I'm trying to bind a GridViewComboBoxColumn to, but the dropdown will only display when I click in the cell and then disappears when I leave. Any help would be greatly appreciated.

Thanks
Scott

<telerik:RadGridView x:Name="radGrid" Margin="5,5,5,5" ItemsSource="{Binding PersonViews}" RowIndicatorVisibility="Collapsed" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontSize="14">

<telerik:RadGridView.Columns>

<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />

<telerik:GridViewDataColumn DataMemberBinding="{Binding Location}" Header="Location"/>

<telerik:GridViewComboBoxColumn ItemsSourceBinding="{Binding Occupation}"

UniqueName="Uniq"

SelectedValueMemberPath="ID"

DisplayMemberPath="Occupation" Header="Occupation"/>

</telerik:RadGridView.Columns>

</telerik:RadGridView>

 

 

using

 

 

System;

 

using

 

 

System.Collections.Generic;

 

using

 

 

System.Linq;

 

using

 

 

System.Text;

 

using

 

 

System.ComponentModel;

 

using

 

 

System.Collections.ObjectModel;

 

namespace

 

 

GridComboBindingTest

 

{

 

 

class GridViewModel : INotifyPropertyChanged

 

{

 

 

private ObservableCollection<PersonView> _personViews;

 

 

 

 

public GridViewModel()

 

{

PersonViews =

 

new ObservableCollection<PersonView>();

 

 

 

PersonView gridView = new PersonView();

 

gridView.Location =

 

"Chicago";

 

gridView.Name =

 

"Fred";

 

 

 

 

PersonOccupation tc = new PersonOccupation();

 

tc.ID = 1;

tc.Occupation =

 

"Programmer";

 

gridView.Occupation =

 

new ObservableCollection<PersonOccupation>();

 

gridView.Occupation.Add(tc);

tc =

 

new PersonOccupation();

 

tc.ID = 2;

tc.Occupation =

 

"Sales";

 

gridView.Occupation.Add(tc);

tc =

 

new PersonOccupation();

 

tc.ID = 3;

tc.Occupation =

 

"Tech";

 

gridView.Occupation.Add(tc);

PersonViews.Add(gridView);

 

 

 

gridView =

 

new PersonView();

 

gridView.Location =

 

"NY";

 

gridView.Name =

 

"Joe";

 

tc =

 

new PersonOccupation();

 

tc.ID = 1;

tc.Occupation =

 

"Cook";

 

gridView.Occupation =

 

new ObservableCollection<PersonOccupation>();

 

gridView.Occupation.Add(tc);

tc =

 

new PersonOccupation();

 

tc.ID = 2;

tc.Occupation =

 

"Chef";

 

gridView.Occupation.Add(tc);

tc =

 

new PersonOccupation();

 

tc.ID = 3;

tc.Occupation =

 

"Tech";

 

gridView.Occupation.Add(tc);

PersonViews.Add(gridView);

 

 

 

gridView =

 

new PersonView();

 

gridView.Location =

 

"LA";

 

gridView.Name =

 

"Sam";

 

tc =

 

new PersonOccupation();

 

tc.ID = 1;

tc.Occupation =

 

"Tech";

 

gridView.Occupation =

 

new ObservableCollection<PersonOccupation>();

 

gridView.Occupation.Add(tc);

tc =

 

new PersonOccupation();

 

tc.ID = 2;

tc.Occupation =

 

"Student";

 

gridView.Occupation.Add(tc);

tc =

 

new PersonOccupation();

 

tc.ID = 3;

tc.Occupation =

 

"Writer";

 

gridView.Occupation.Add(tc);

PersonViews.Add(gridView);

}

 

 

 

public ObservableCollection<PersonView> PersonViews

 

{

 

 

get { return _personViews; }

 

 

 

set

 

{

 

 

if (_personViews != value)

 

{

_personViews =

 

value;

 

RaisePropertyChanged(

 

"GridViews");

 

}

}

}

 

 

 

public event PropertyChangedEventHandler PropertyChanged;

 

 

 

private void RaisePropertyChanged(string propertyName)

 

{

 

 

// take a copy to prevent thread issues

 

 

 

PropertyChangedEventHandler handler = PropertyChanged;

 

 

 

if (handler != null)

 

{

handler(

 

this, new PropertyChangedEventArgs(propertyName));

 

}

}

}

 

 

 

public class PersonView : INotifyPropertyChanged

 

{

 

 

private ObservableCollection<PersonOccupation> _occupation;

 

 

 

public string Name

 

{

 

 

get;

 

 

 

set;

 

}

 

 

public string Location

 

{

 

 

get;

 

 

 

set;

 

}

 

 

 

public ObservableCollection<PersonOccupation> Occupation

 

{

 

 

get { return _occupation; }

 

 

 

set

 

{

 

 

if (_occupation != value)

 

{

_occupation =

 

value;

 

RaisePropertyChanged(

 

"Person");

 

}

}

}

 

 

public event PropertyChangedEventHandler PropertyChanged;

 

 

 

private void RaisePropertyChanged(string propertyName)

 

{

 

 

// take a copy to prevent thread issues

 

 

 

PropertyChangedEventHandler handler = PropertyChanged;

 

 

 

if (handler != null)

 

{

handler(

 

this, new PropertyChangedEventArgs(propertyName));

 

}

}

}

 

 

public class PersonOccupation : INotifyPropertyChanged

 

{

 

 

private string _occupation;

 

 

 

private int _ID;

 

 

 

public string Occupation

 

{

 

 

get { return _occupation; }

 

 

 

set

 

{

 

 

if (_occupation != value)

 

{

_occupation =

 

value;

 

RaisePropertyChanged(

 

"Occupation");

 

}

}

}

 

 

public int ID

 

{

 

 

get { return _ID; }

 

 

 

set

 

{

 

 

if (_ID != value)

 

{

_ID =

 

value;

 

RaisePropertyChanged(

 

"ID");

 

}

}

}

 

 

public event PropertyChangedEventHandler PropertyChanged;

 

 

 

private void RaisePropertyChanged(string propertyName)

 

{

 

 

// take a copy to prevent thread issues

 

 

 

PropertyChangedEventHandler handler = PropertyChanged;

 

 

 

if (handler != null)

 

{

handler(

 

this, new PropertyChangedEventArgs(propertyName));

 

}

}

}

}

8 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 31 Jul 2012, 05:42 AM
Hello Scott,

I would suggest you to check the problem explained in this help article.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Scott Michetti
Top achievements
Rank 1
Iron
answered on 01 Aug 2012, 01:59 PM
Thanks Didie. I actually found what I need in the attached project in this forum. http://www.telerik.com/community/forums/silverlight/gridview/binding-from-radgridview-to-radcombobox.aspx
The only issue I have now is that when I change a value in the combobox, the property changed event does not fire. If you can tell me what I need to do to get that to work I would appreciate it.

Thanks
Scott
0
Dimitrina
Telerik team
answered on 02 Aug 2012, 11:33 AM
Hi Scott,

Generally the PropertyChanged event will be raised when the row currently being edited is committed. May I ask you to share what is the exact scenario you would like to achieve?

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Scott Michetti
Top achievements
Rank 1
Iron
answered on 02 Aug 2012, 02:33 PM
Thanks Didie. The event never fires. Changing the name or the number causes those events to fire, but changing the combo never causes the event to fire, even when I leave the row. I'd like to save values to a database after the values have been chaged, so I need to trap the event for that.

Thanks
Scott
0
Dimitrina
Telerik team
answered on 02 Aug 2012, 03:53 PM
Hi Scott,

 The project you reference is actually not working the right way. I have attached a new project showing how to achieve your goal. You can check that the PropertyChanged event will be raised after a change made in the second column is committed.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Scott Michetti
Top achievements
Rank 1
Iron
answered on 02 Aug 2012, 04:59 PM
Hello Didie, the project I referenced was used because each combobox had its own itemsource. I need to have a different list in each combobox, so that's why I used it. The project you include does have the event fire, but it only has 1 list for the whole column. Can I have a different list in each combo and have the event fire? I tried the following code in the project I referenced so I can have a combobox in the cell.

<telerik:GridViewDataColumn DataMemberBinding="{Binding ClubID}" Header="Template Column">
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <telerik:RadComboBox SelectedValuePath="ID" 
                                                 SelectedValue="{Binding ClubID, Mode=TwoWay}" 
                                                 ItemsSource="{Binding AvailableClubs}"
                                                 DisplayMemberPath="Name" SelectionChanged="RadComboBox_SelectionChanged" />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
                    </telerik:GridViewDataColumn>
Thanks
Scott
0
Pavel Pavlov
Telerik team
answered on 06 Aug 2012, 08:44 AM
Hello Scott ,

I have prepared a simplified sample , where each row has its own list of items for the combo.
It has the setter correctly invoced and corractly raises property changed events.

Please find the project attached.


Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Scott Michetti
Top achievements
Rank 1
Iron
answered on 07 Aug 2012, 03:18 PM
Thanks Pavel. That works for me. I was hoping to get a pure MVVM way of making this work, but code behind gets the job done.

Thanks for all your help.
Scott
Tags
GridView
Asked by
Scott Michetti
Top achievements
Rank 1
Iron
Answers by
Dimitrina
Telerik team
Scott Michetti
Top achievements
Rank 1
Iron
Pavel Pavlov
Telerik team
Share this question
or