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

Data binding problem

2 Answers 33 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ali Rafiee
Top achievements
Rank 1
Ali Rafiee asked on 08 Jul 2012, 12:21 AM
Hi,

I'm trying to bind an ObservableCollection to a RadGridView, but I don't see any rows in the GridView.  I've read the documentation and seen the sample and can't figure out what could be wrong!

The sample is a simple sample
public class SalesPerson
{
    public string Name { get; set; }
    public double SoldAmount { get; set; }
}
 
public partial class MainPage : UserControl
{
    public ObservableCollection<SalesPerson> SalesPeople { get; set; }
 
    public MainPage()
    {
        InitializeComponent();
 
        SalesPeople = new ObservableCollection<SalesPerson>();
        SalesPeople.Add(new SalesPerson() { Name = "Ali Rafiee", SoldAmount = 12.00 });
        SalesPeople.Add(new SalesPerson() { Name = "Neda Rafiee", SoldAmount = 123.00 });
        SalesPeople.Add(new SalesPerson() { Name = "Ryan Rafiee", SoldAmount = 1234.00 });
    }

Here is the xaml:
<telerik:RadGridView Margin="12,12,12,0" ItemsSource="{Binding SalesPeople}" VerticalAlignment="Top" AutoGenerateColumns="False">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}"/>
        <telerik:GridViewDataColumn Header="Amount" DataMemberBinding="{Binding SoldAmount}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

I feel like I'm missing something really simple.

Thanks,
Ali

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 09 Jul 2012, 05:21 AM
Hi,

 The data is not populated as the SalesData collection could not be found in the DataContext of the RadGridView.

You could set the ItemsSource in code behind or set the DataContext similar to:

public MainPage()
       {
           InitializeComponent();
           ...
 
           this.DataContext = this;
       }

Hope this is what you need.

Kind regards,
Didie
the Telerik team

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

0
Ali Rafiee
Top achievements
Rank 1
answered on 09 Jul 2012, 10:37 PM
Thanks.  I knew it was something stupidly simple.  I was under the impression that by default the DataContext was set to the code behind object. (In real life I use VMs.)
Tags
GridView
Asked by
Ali Rafiee
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Ali Rafiee
Top achievements
Rank 1
Share this question
or