Binding RadGridview MVVM headaches

2 posts, 0 answers
  1. Randy Hompesch
    Randy Hompesch avatar
    148 posts
    Member since:
    Nov 2008

    Posted 13 Nov 2016 Link to this post

    I'm just getting into this and I just can't figure out what I am doing wrong.

    I have the following XAML (at least the relevant part) followed by the code in the Viewmodel to load it.

    In the seachcommand function I make a db call and load the results into my observable collection of SearchResultBag.

    The query runs fun and I get data. It just doesn't show in the grid. What is the magic sauce I am missing. This seems like it should be trivial.

    If I get rid of the SearchResultsBag and ONLY return a string (for testing purposes) the string(s) get displayed in the grid. Why won't my SearchResultsBag display it's contents. I've been a good boy and defined the template columns. It's gotta be something simple but I just can't see what I am missing.

    While I'm at it, can anyone point me to a good MVVM databound RadGridView example. The telerik demo app is WAAAAY to complicated for a newb like me to pick up the basics from. I'm sure if I can get this simple search thing working I can get the rest.

    Thanks ... Ed

     

    <Button x:Name="cmdSearch"  Grid.Row="8" Grid.Column="1"        Content="Search" HorizontalAlignment="Left"         Command="{Binding SearchCommand }" Margin="0,5,0,5"/>


    <telerik:RadGridView Grid.Row="9" Grid.Column="0" Grid.ColumnSpan="3" x:Name="ssSearch"
         Visibility="{Binding ShowResults, Converter={StaticResource bResultsVisibleConverter}}"
         ItemsSource="{Binding SearchResults, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay }"
         Height="200"
                         >
                 <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="Request #" DataMemberBinding="{Binding CustomKey}"/>
                    <telerik:GridViewDataColumn Header="ClientName" DataMemberBinding="{Binding ClientName}"/>
                    <telerik:GridViewDataColumn Header="ClientId" DataMemberBinding="{Binding ClientId}"/>
                    <telerik:GridViewDataColumn Header="OpenedBy" DataMemberBinding="{Binding OpenedBy}"/>
                    <telerik:GridViewDataColumn Header="ProjectId" DataMemberBinding="{Binding ProjectId}"/>
                    <telerik:GridViewDataColumn Header="OpenedDate" DataMemberBinding="{Binding OpenedDate}"/>
                    <telerik:GridViewDataColumn Header="RequestedDate" DataMemberBinding="{Binding RequestedDate}"/>
                    <telerik:GridViewDataColumn Header="CloseDate" DataMemberBinding="{Binding CloseDate}"/>


                </telerik:RadGridView.Columns>
            </telerik:RadGridView>

     

     

    SearchCommand = new Prism.Commands.DelegateCommand(Search).ObservesProperty(() => SearchResults);
    private ObservableCollection<SearchResultsBag> _searchResults;

    public ObservableCollection<SearchResultsBag> SearchResults
    {
    get { return _searchResults; }
    set { SetProperty(ref _searchResults, value); }
    }
    private void Search()
    {
    this.SearchResults = new ObservableCollection<SearchResultsBag>();
    var q = from a in db.CALMS_Request
    where a.Custom_Key == CustomKey
    select new SearchResultsBag
    {
    CustomKey = a.Custom_Key,
    ClientName = a.Client.ClientName,
    ClientId = a.Client.ClientId.ToString(),
    OpenedBy = "OpenedBy",
    ProjectId = a.ProjectID,
    OpenDate = a.DateCreated.ToString(),
    RequestedDate = a.RequestedDate,
    CloseDate = a.ClosedDate
    };
    if (string.IsNullOrWhiteSpace(CustomKey))
    q = q.Where(a => a.CustomKey.ToLower() == CustomKey.ToLower());


    foreach (SearchResultsBag item in q)
    this.SearchResults.Add(item);
    ShowResults = true;
    }
    public class SearchResultsBag
    {
    public string CustomKey;
    public string ClientName;
    public string ClientId;
    public string OpenedBy;
    public string ProjectId;
    public string OpenDate;
    public string RequestedDate;
    public string CloseDate;
    }

     

  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    665 posts

    Posted 14 Nov 2016 Link to this post

    Hello Randy,

    I have attached a sample project that follows the MVVM pattern. We generally use it when our customers contact us for some issues so it is pretty simple and you might come across it in some other threads. 

    As for the actual issue you are experiencing, since you have not included the full implementation from your end, can you please have a look at the RadGridView`s ItemsSource once the control is loaded and update me whether the expected items are present? Simply attach to the RadGridView`s loaded event and have a look at the source collection(SearchResults). If the collection is not empty, can you have a look at the Items collection of the RadGridView once the control is loaded?

    Actually, the best case scenario would be to raise a ticket with a sample application that shows the undesired behavior so we can have a more detailed look and advise you more appropriately.

    Regards,
    Stefan Nenchev
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
Back to Top