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

binding to a list within list

7 Answers 340 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Deepjot Khurana
Top achievements
Rank 1
Deepjot Khurana asked on 17 Sep 2010, 12:15 AM
I have a very simple problem. I have a list of cars and each car can further contain its name and a list of parts that consist the car. Now i want to show the cars(names) in a grid and the corresponding parts in a combobox in the same row using GridViewComboBoxColumn.
I have set the itemsource property of grid to Cars. Then i used the GridViewDataColumn's unique name to display the name and item source property of GridViewComboBoxColumn to display parts. Here is the code

 

 

 

 

 

<

 

telerik:RadGridView Name="UserListGrid" DockPanel.Dock="Top" Height="520"

 

 

ScrollMode="RealTime"

 

 

UseAlternateRowStyle="True"

 

 

AutoGenerateColumns="False"

 

 

ShowGroupPanel="False"

 

 

IsReadOnly="False"

 

 

RowIndicatorVisibility ="Visible"

 

 

CanUserFreezeColumns="False"

 

 

SelectionMode="Single"

 

 

ItemsSource="{Binding OperatorInfoItems, Mode=TwoWay}"

 

 

SelectedItem="{Binding SelectedRow, Mode=TwoWay}"

 

 

IsSynchronizedWithCurrentItem="True"

 

 

SelectionChanged="UserListGrid_SelectionChanged">

 

 

 

<telerik:RadGridView.Columns>

 

 

 

<telerik:GridViewDataColumn UniqueName="Cars" DataType="{x:Null}" Header="Names" IsVisible="True" IsFilterable="False" IsGroupable="False" IsReadOnly="True" IsSortable="False" Width="150"/>

 

 

 

<telerik:GridViewComboBoxColumn ItemsSource="{Binding Parts}" Header="Parts"/>

 

 

 

</telerik:RadGridView.Columns>

 

 

 

</telerik:RadGridView>

 

 

 

 

 

But after running the project i could only see names and a data binding error message instead of a populated combobox.
Pl tell me what to do.

 

 

 

 

 

 

 

 

 

 

7 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 17 Sep 2010, 09:54 AM
Hi Deepjot Khurana,

Please paste me the code behind implementation  of your business objects - car and part , and I will be glad to integrate them  in a working example for you .

All the best,
Pavel Pavlov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Deepjot Khurana
Top achievements
Rank 1
answered on 17 Sep 2010, 06:09 PM

Its very difficult to publish here the whole code. But the following code is being used for most of the part:

public interface ICarsList
{
 string Name{ get; set; }
 List<string> Parts { get; set; }
}

public class CarsList: INotifyPropertyChanged, ICarsList
{
 _private List<string> _parts;
 _private string _name;

  
 public List<string> Parts
        {
            get { return _parts}
            set
            {
                if (value != _parts)
                {
                   _parts = value;
                    OnPropertyChanged("Parts");
                }
            }

        }
 public string  Name
        {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                     _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }
 public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

}

Class CarsListViewModel:ObservableObject, INotifyPropertyChanged
{

 public ObservableCollection<ICarsList> Cars { get; set; }// dont need to set though
 public CarsListViewModel()
 {
  Cars = new ObservableCollection<ICarsList>(_dataSource.Getdata());// data is coming correctly. Parts is populated too
 }

}

////Cars is correctly populated and its member Name is correctly displayed in the grid too but i am not able to show parts in the combobox. I tried specifying the correct binding in the GridViewComboBoxColumn but still getting data binding errors.

Thanks
Deepjot

 

 

 

 

 

 


0
Maya
Telerik team
answered on 22 Sep 2010, 10:47 AM
Hi Deepjot Khurana,

Basically, you need to set the DataMemberBinding Property of the GridViewComboBoxColumn. You may take a look at our online documentation for further reference about that type of column. 
Furthermore, I am sending you a sample project illustrating the implementation of the GridViewComboBoxColumn.

Greetings,
Maya
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Deepjot Khurana
Top achievements
Rank 1
answered on 24 Sep 2010, 08:24 PM
Thanks Maya for your prompt reply. 
But my question was what if a list (pointed by itemsource of grid) contains another dyanamic list. In the context of the sample project you attached, my question would become: what if Person object contains many countryIDs instead of one and i want to display these countryIDs now in the

GridViewComboBoxColumn

 

. I have changed your Person class and GetNames() method to this:

public

 

class Person : INotifyPropertyChanged

 

{

 

private string name;

 

 

private List<int> countryID;

 

 

 

public string Name

 

{

 

get

 

{

 

return this.name;

 

}

 

set

 

{

 

if (this.name != value)

 

{

 

this.name = value;

 

 

this.OnPropertyChanged("Name");

 

}

}

}

 

private int age;

 

 

public int Age

 

{

 

get

 

{

 

return this.age;

 

}

 

set

 

{

 

this.age = value;

 

 

this.OnPropertyChanged("Age");

 

}

}

 

 

public List<int> CountryIDs

 

{

 

get

 

{

 

return this.countryID;

 

}

 

set

 

{

 

if (this.countryID != value)

 

{

 

this.countryID = value;

 

 

this.OnPropertyChanged("CountryID");

 

}

}

}

 

 

public Person()

 

{}

 

public Person(string name, List<int> countryID, int age)

 

{

 

this.name = name;

 

 

this.countryID = countryID;

 

 

this.age = age;

 

}

 

public static ObservableCollection<Person> GetNames()

 

{

    ObservableCollection<Person> names =

new ObservableCollection<Person>();

 

 

 

    List<int> l1= new List<int>();

 

    l1.Add(2);

 

 

    List<int> l2= new List<int>();
    l2.Add(3);
    l2.Add(4);

    List<int> l3= new List<int>();
    l3.Add(5);
    l3.Add(6);
    l3.Add(7);


    names.Add(

new Person("Adam Debrovski", l1, 14));

 

    names.Add(

new Person("Juames Peterson", l2, 16));

 

    names.Add(

new Person("Vladislav Znekovski", l3, 17));

 

 

    return names;

 

}


Now, i want that in each GridViewRow, Person Name, Age and associated CountryIDs(in GridViewComboBoxColumn) should appear, and also that the first CountryID in the list should be selected automatically.

Thanks,
Deepjot

0
Maya
Telerik team
answered on 27 Sep 2010, 05:30 PM
Hello Deepjot Khurana,

If I understand you correctly, you require every row to have a separate set of items for the ComboBox. If that is the case, please take a look at this blog post and let me know if it is similar and corresponds to your scenario. 
 

Kind regards,
Maya
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Deepjot Khurana
Top achievements
Rank 1
answered on 27 Sep 2010, 05:57 PM
Thanks Maya for your promptness. But the blogpost handles the problem of populating cascading comboboxes(based on selection) that is not what i wanted.

The problem is very simple. Let me rephrase my problem. I have a list within a list that is a list of cars and each car further contains a list of parts; and i want to use two columns: a datacolumn to display car name and a combobox to display Parts of the car. The requirement is that when the screen containing gridview appears, it should show the name of each car in one column and name of the first part in second column which is also a combobox so that user may see all other parts by expanding it.
P.S: Nothing depends on selection of anything.

Thanks,
Deepjot
0
Maya
Telerik team
answered on 28 Sep 2010, 01:36 PM
Hello Deepjot Khurana,

Basically, you need to implement the same idea in your application as the one demonstrated in the blog post. The main part there that corresponds to your requirements is:

public IEnumerable<Country> AvailableCountries
{
      get
      {
                return from c in Locations.Countries
                       where c.ContinentCode == this.ContinentCode
                       select c;
      }
}

This specifies that the ItemsSource for the RadComboBox in each row is loaded according to some filtering logic. In your case, the idea will be to filter the items in the comboBox depending on the name of the car's name.
I am sending you a sample project illustrating the proposed solution.
 


Sincerely yours,
Maya
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Deepjot Khurana
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Deepjot Khurana
Top achievements
Rank 1
Maya
Telerik team
Share this question
or