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

Bind class to RadGridView

6 Answers 273 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dennis van Espelo
Top achievements
Rank 1
Dennis van Espelo asked on 25 Oct 2012, 02:33 PM
Hi,

I am having trouble binding a class with 2 members to a RadGridView. The first member contains a dictionary to set the column header text and the second to fill the grid.

here is my sample project

cs

public Content()
{
      
    // Get the labels from database
    ContextSource.Labels = DictionaryFromDatabase();
    // Get grid data from database
    ContextSource.DataSrc = DataTableFromDatabase();
      
    Listing.DataContext = ContextSource;
}
  
public class ContextSource
{
  
    public DataTable DataSrc { get; set; }
    public Dictionary<string, string> Labels { get; set; }
      
    public ContextSource()
    {
        DataSrc = new DataTable();
        Labels = new Dictionary<string, string>;
    }
}

xaml
<telerik:RadGridView Margin="0"
                              
    AreRowDetailsFrozen="True"
    AutoGenerateColumns="False"
    CanUserFreezeColumns="False"
    IsReadOnly="True"
    ItemsSource="{Binding}" 
    SelectionMode="Single"
    ShowInsertRow="False"
    x:Name="Listing">
  
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="{Binding Labels.[str_label0]}" DataMemberBinding="{Binding Path=DataSrc.[columnname0]}"/>
        <telerik:GridViewDataColumn Header="{Binding Labels.[str_label1]}" DataMemberBinding="{Binding Path=DataSrc.[columnname1]}"/>
        <telerik:GridViewDataColumn Header="{Binding Labels.[str_label2]}" DataMemberBinding="{Binding Path=DataSrc.[columnname2]}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

When I set Listing.DataContext = ContextSource, it will only fill the header text property. The grid is not populated.

If I direct the Listing.DataContent directly to ContextSource.DataSrc then the grid is populated, but the headers are not set.
Listing.DataContext = ContextSource.DataSrc;


Any idea what I am doing wrong?

Kind regards,
Dennis


6 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 25 Oct 2012, 03:21 PM
Hi,

Please try the following :
1. Leave the DataContext of the grid to be ContextSource;

2. Modify the ItemsSource of the grid , adding a path pointing to  the DataSrc property .

something like :
<telerik:RadGridView Margin="0"
                                
    AreRowDetailsFrozen="True"
    AutoGenerateColumns="False"
    CanUserFreezeColumns="False"
    IsReadOnly="True"
    ItemsSource="{Binding Path=DataSrc}" 
    SelectionMode="Single"
    ShowInsertRow="False"
    x:Name="Listing"


Let me know in case this does not work.

Greetings,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Dennis van Espelo
Top achievements
Rank 1
answered on 26 Oct 2012, 07:20 AM
Using ItemsSource="{Binding Path=DataSrc}"  works. The grid is now populated, except now the headers lost their binding.

The columnnames are set to columnname0, columnname1 and columnname2.
0
Dennis van Espelo
Top achievements
Rank 1
answered on 05 Nov 2012, 09:24 AM
Any idea how to solve this?
0
Pavel Pavlov
Telerik team
answered on 05 Nov 2012, 01:01 PM
Hello,
Here is how to solve this :

Please expose the ContextSource as a StaticResource with a key in your window resources.
When binding the header to it use binding to a Static Resource .

This will solve the problem.

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Dennis van Espelo
Top achievements
Rank 1
answered on 06 Nov 2012, 12:56 PM
Would it be possible to provide a sample?

Thanks,
Dennis


0
Maya
Telerik team
answered on 12 Nov 2012, 04:52 PM
Hi Dennis,

You can try something as follows:

<Window.Resource>
  <my:ContextSource x:Key="ContextSource" />
</Window.Resource>
 
.......
Header="{Binding MyProperty, Source={StaticResource ContextSource}}"
 

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Dennis van Espelo
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Dennis van Espelo
Top achievements
Rank 1
Maya
Telerik team
Share this question
or