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

[Solved] Example of binding a DataGrid to a Data Storage

6 Answers 147 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rajesh
Top achievements
Rank 1
Rajesh asked on 04 Jun 2014, 07:49 AM
Hi,

In this article http://www.telerik.com/help/windows-8-html/grid-binding-to-data-storage.html it explains how to bind the Grid to a Telerik Data Storage. (http://blogs.telerik.com/windows8team/posts/13-06-13/data-storage-for-windows-store-apps---a-walkthrough)

I would want to know how to achieve the same in XAML with a RAD Data Grid and Data Storage.

Can you please let me know if it is documented anywhere.

What I am interested in is the query should be in SQL so that I can define the join between tables and then populate the grid based on that data. That is how most real time applications work instead of binding to a single table.

Thanks,

Rajesh

 

6 Answers, 1 is accepted

Sort by
0
Santiago
Top achievements
Rank 1
answered on 04 Jun 2014, 09:15 PM
Admin can probably merge this with http://www.telerik.com/forums/grid-binding-to-data-storage as I'm interested in exactly the same question, and its the one missing aspect of telerik's package being ideal for my project.
0
Rosy Topchiyska
Telerik team
answered on 05 Jun 2014, 02:23 PM
Hello,

Thank you for contacting us.

This functionality is in our product backlog for the next official release of the controls Q2 2014, planned for the end of June. Our developers will do their best to implement it.

Please, let us know if you have any other questions.

Regards,
Rosy Topchiyska
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Santiago
Top achievements
Rank 1
answered on 05 Jun 2014, 05:55 PM
So there is no current work around? i.e. intercept changes from the radgrid and update the data storage manually?  I had a few methods mapped out for doing it but I wanted to check in first to see if there was an official way to do it.  The 1st draft of the project I"m working on is due by the end of June.  Any help in the right direction would be appreciated.
0
Rosy Topchiyska
Telerik team
answered on 10 Jun 2014, 02:17 PM
Hello Santiago,

You can take a look at our XAML Data Storage Demo that demonstrates how you can use the Data Storage and the RadDataGrid control together. You can download the demo application from this address when you are logged into your account: http://www.telerik.com/account/your-products/trial-product-versions/trial-single-download.aspx?pmvid=3636&pid=1216

I hope this helps. Please, let us know if you have further questions.

Regards,
Rosy Topchiyska
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Santiago
Top achievements
Rank 1
answered on 11 Jun 2014, 07:08 PM
MainPage.xaml
<Page
    xmlns:local="using:SimpleDataBinding"
    xmlns:Grid="using:Telerik.UI.Xaml.Controls.Grid"
    x:Class="SimpleDataBinding.MainPage"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Modify" HorizontalAlignment="Left" Margin="1036,152,0,0" VerticalAlignment="Top" Height="90" Width="200" Click="Button_Click_1"/>
        <Button x:Name="Delete" Content="Delete" HorizontalAlignment="Left" Margin="742,152,0,0" VerticalAlignment="Top" Height="90" Width="200" Click="Delete_Click"/>
        <Button x:Name="Add" Content="Add" HorizontalAlignment="Left" Margin="436,152,0,0" VerticalAlignment="Top" Height="90" Width="200" Click="Add_Click"/>
        <Grid:RadDataGrid Name="grid1" HorizontalAlignment="Left" Margin="10,329,0,0" VerticalAlignment="Top" Height="242" Width="1346" UserEditMode="Inline"/>
 
    </Grid>
</Page>

001.MainPage.Xaml.cs
002. 
003.using System;
004.using System.Collections.Generic;
005.using System.Collections.ObjectModel;
006.using System.IO;
007.using System.Linq;
008.using System.Runtime.InteropServices.WindowsRuntime;
009.using Windows.Foundation;
010.using Windows.Foundation.Collections;
011.using Windows.UI.Xaml;
012.using Windows.UI.Xaml.Controls;
013.using Windows.UI.Xaml.Controls.Primitives;
014.using Windows.UI.Xaml.Data;
015.using Windows.UI.Xaml.Input;
016.using Windows.UI.Xaml.Media;
017.using Windows.UI.Xaml.Navigation;
018.using Telerik.Storage.Extensions;
019. 
020. 
021.using System.ComponentModel.DataAnnotations;
022.using System.ComponentModel.DataAnnotations.Schema;
023. 
024.// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
025. 
026.namespace SimpleDataBinding
027.{
028.    // A simple business object
029.    public class Recording : System.ComponentModel.INotifyPropertyChanged
030.    {
031.        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
032. 
033.        protected virtual void OnPropertyChanged(string propertyName)
034.        {
035.            if (this.PropertyChanged != null)
036.            {
037.                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
038.            }
039.        }
040.        public Recording() { }
041. 
042. 
043.        [Key]
044.        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
045.        public int DataKey { get; set; }
046. 
047.        public Recording(string artistName, string cdName, DateTime release)
048.        {
049.            Artist = artistName;
050.            Name = cdName;
051.            ReleaseDate = release;
052.        }
053. 
054.        private string _Artist;
055.        public string Artist
056.        {
057.            get
058.            {
059.                return _Artist;
060.            }
061.            set
062.            {
063.                if (this._Artist != value)
064.                {
065.                    this._Artist = value;
066.                    this.OnPropertyChanged("Artist");
067.                
068.            }
069.        }
070.        public string Name { get; set; }
071.        public DateTime ReleaseDate { get; set; }
072. 
073.        // Override the ToString method.
074.        public override string ToString()
075.        {
076.            return Name + " by " + Artist + ", Released: " + ReleaseDate.ToString("d");
077.        }
078.    }
079. 
080.    /// <summary>
081.    /// An empty page that can be used on its own or navigated to within a Frame.
082.    /// </summary>
083.    public sealed partial class MainPage : Page
084.    {
085.        public ObservableCollection<Recording> MyMusic = new ObservableCollection<Recording>();
086.        public Context context;
087. 
088.        System.Linq.IQueryable<Recording> allMusic;
089. 
090.        public MainPage()
091.        {
092.            this.InitializeComponent();
093. 
094.            //This is not a good practice in general.
095.            context = new Context("generalTest", DatabaseLocation.Local);
096.            resetData();
097. 
098.        }
099. 
100.        public void resetData()
101.        {
102.            int total = (from item in context.GetAll<Recording>() select item).Count();
103.            if (total <= 1)
104.            {
105.                // Add items to the collection.
106.                context.Insert<Recording>(new Recording("Luka Abrus",
107.                    "The Road to Redmond", new DateTime(2007, 4, 3)));
108.                context.Insert<Recording>(new Recording("Jim Hance",
109.                    "The Best of Jim Hance", new DateTime(2007, 2, 6)));
110.                context.Insert<Recording>(new Recording("Chris Sells", "Chris Sells Live",
111.                    new DateTime(2008, 2, 5)));
112.                context.SaveChanges();
113.            }
114.            allMusic = from item in context.GetAll<Recording>() select item;
115.            foreach (Recording anItem in allMusic)
116.            {
117.                anItem.PropertyChanged += somethingChanged;
118.            }
119. 
120.            MyMusic = new ObservableCollection<Recording>(allMusic);
121. 
122.            grid1.ItemsSource = MyMusic;
123.        }
124. 
125.        public void somethingChanged(object sender, System.ComponentModel.PropertyChangedEventArgs args)
126.        {
127.            Recording temp = sender as Recording;
128.            context.Update<Recording>(temp);
129.            context.SaveChanges();
130. 
131.        }
132. 
133.        private void Button_Click_1(object sender, RoutedEventArgs e)
134.        {
135.            MyMusic.First().Artist = "Demented";
136.        }
137. 
138.        private void Delete_Click(object sender, RoutedEventArgs e)
139.        {
140. 
141.            if (MyMusic.Count > 0)
142.            {
143.                Recording temp = MyMusic[0];
144.                context.Delete<Recording>(temp);
145.                context.SaveChangesAsync();
146.                MyMusic.Remove(temp);
147.            }
148.            else
149.                resetData();
150.        }
151. 
152.        private void Add_Click(object sender, RoutedEventArgs e)
153.        {
154.            Recording temp = new Recording("Crazy Time", "Chris Sells Live",
155.    new DateTime(2008, 2, 5));          
156.            context.Insert<Recording>(temp);
157.            context.SaveChanges();
158.            MyMusic.Add(context.GetAll<Recording>().Last<Recording>());
159. 
160.             
161.        }
162.    }
163.}

So this is as close as I can get to what I originally wanted to happen.  First off I don't normally put everything in the view, but I just wanted something demonstrable.  I looked at the XAML Data Storage Demo, but I really wanted to be able to use the built-in radgrid modification system.  Radgrid works fine in this regard when bound to a regular datasource, so the idea is that I just need to keep the 2 sources synchronized.  This is done by implementing INotifyPropertyChanged in my data object.  Whenever that is triggered the somethingChanged function updates the data source.  

The only part that feels hacky to me is the add function as I have to add the item to the Telerik Data storage and wait for that to complete before adding it to the local storage, since I want to store the data key that is generated.  and having to call context.getAll to be able to call last.

I haven't looked into any performance issues, but it works.  I can make inline edits, add and delete objects and have it persist.
0
Rosy Topchiyska
Telerik team
answered on 16 Jun 2014, 10:45 AM
Hello Santiago,

I want to inform you that on June 18 we will release Q2 2014 of the Telerik UI for Windows 8 that will include a DataProvider which will act as a contract between the RadDataGrid and the DatStorage controls and it will allow the edit->save flow out of the box.

Please, let is know should you have any questions.

Regards,
Rosy Topchiyska
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid for XAML
Asked by
Rajesh
Top achievements
Rank 1
Answers by
Santiago
Top achievements
Rank 1
Rosy Topchiyska
Telerik team
Share this question
or