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

Binding RadDataForm to Observable Collection with LINQ

0 Answers 211 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
visionharvest
Top achievements
Rank 1
visionharvest asked on 16 Jul 2011, 05:00 PM
Hi,

I'm something of a beginner and have built a WPF window that uses the RadDataForm. Everything works, so I don't have a specific problem, but am looking to see if there is a better solution than the one I've created.

Essentially, I'm creating an ObservableCollection and filling it with one record from the database using LINQ. My problem was that changes made to the OC in the dataform at runtime were not getting written back to the database. After a lot of googling, I have become thouroughly confused on the how to properly set this up, so came up with my own idea. I'm posting the code more as a peer review exercise rather than looking for a fix to an error.

The big question here is: Is there a better approach than the one I used?

AcctInfo.xaml.cs
using System.Linq;
using System.Windows;
using NAPAData;
using System.Collections.ObjectModel;
namespace wpfNAPADashBoard.Pages
{
/// <summary>
/// Interaction logic for AcctInfo.xaml
/// </summary>
public partial class AcctInfo : Window
{
//Make a connection to the database Entity class context.
TAMSdbDataContext db = new TAMSdbDataContext();
//Access the user name saved to the iDictionary as part of the login process.
string uName = (string)App.Current.Properties["UserName"];
//Create an observable collection to bind the raddataform to. The collection is empty at this point.
ObservableCollection<UserDetail> obCol = new ObservableCollection<UserDetail>();
public AcctInfo()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//upon loading the window - LINQ query to populate the observable collection with a single record from the database
var userDetails =
from ud in db.UserDetails
where ud.UserName == uName
select ud;
foreach (var i in userDetails)
{
obCol.Add(i);
}
//Binds the observable collection to the dataform
AccountInfo.ItemsSource = obCol.ToList();
}
private void RadMenuItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
//Used by the RadMenu to close the window popup
this.Close();
}
private void AccountInfo_EditEnded(object sender, Telerik.Windows.Controls.Data.DataForm.EditEndedEventArgs e)
{
//AccountInfo_EditEnded is used to save changes made to the observable collection to the database record using LINQ.
//First perform a LINQ query against the observable collection.
var result = (from o in obCol
select o);
//Now loop through the "result" query.
foreach (var x in result)
{
//LINQ query to pull the matching observable collection record from the database.
var userDetails =
from ud in db.UserDetails
where ud.UserName == uName
select ud;
//Apply the observable collection values to the LINQ query values.
foreach (var i in userDetails)
{
i.FirstName = x.FirstName;
i.LastName = x.LastName;
i.CompanyName = x.CompanyName;
i.Address1 = x.Address1;
i.Address2 = x.Address2;
i.Address3 = x.Address3;
i.City = x.City;
i.Region = x.Region;
i.Country = x.Country;
i.Phone = x.Phone;
i.WebSite = x.WebSite;
i.Email = x.Email;
//Save changes to the database
db.SubmitChanges();
}
}
}
}
}

AcctInfo.xaml
<Window x:Class="wpfNAPADashBoard.Pages.AcctInfo"
        Title="Account Information"
         WindowStartupLocation="CenterScreen"
        Height="419"
        Width="806"
        Loaded="Window_Loaded">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="MyTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <telerik:DataFormDataField Grid.Row="0" Grid.Column="0" Label="Client ID:" DataMemberBinding="{Binding ClientID, Mode=OneWay}" IsEnabled="False" />
                    <telerik:DataFormDataField Grid.Row="1" Grid.Column="0" Label="Company Name:" DataMemberBinding="{Binding CompanyName, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="2" Grid.Column="0" Label="Address 1:" DataMemberBinding="{Binding Address1, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="3" Grid.Column="0" Label="Address 2:" DataMemberBinding="{Binding Address2, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="4" Grid.Column="0" Label="Address 3:" DataMemberBinding="{Binding Address3, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="5" Grid.Column="0" Label="City:" DataMemberBinding="{Binding City, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="6" Grid.Column="0" Label="State/Region:" DataMemberBinding="{Binding Region, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="7" Grid.Column="0" Label="PostalCode:" DataMemberBinding="{Binding PostalCode, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="8" Grid.Column="0" Label="Country:" DataMemberBinding="{Binding Country, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="0" Grid.Column="1" Label="Company ID:" DataMemberBinding="{Binding CompanyID, Mode=OneWay}" IsEnabled="False" />
                    <telerik:DataFormDataField Grid.Row="1" Grid.Column="1" Label="First Name:" DataMemberBinding="{Binding FirstName, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="2" Grid.Column="1" Label="Last Name:" DataMemberBinding="{Binding LastName, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="3" Grid.Column="1" Label="Email:" DataMemberBinding="{Binding Email, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="4" Grid.Column="1" Label="Phone:" DataMemberBinding="{Binding Phone, Mode=TwoWay}" />
                    <telerik:DataFormDataField Grid.Row="5" Grid.Column="1" Label="Website:" DataMemberBinding="{Binding WebSite, Mode=TwoWay}" />
                     
                     
                </Grid>
            </DataTemplate>
        </Grid.Resources>
 
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <telerik:RadMenu Name="radMenu1"
                         VerticalAlignment="Top"
                         ClickToOpen="True"
                         telerik:StyleManager.Theme="Office_Black"
                         Grid.Row="0">
            <telerik:RadMenuItem Header="Close" Click="RadMenuItem_Click" />
        </telerik:RadMenu>
 
        <telerik:RadDataForm x:Name="AccountInfo"
                             AutoGenerateFields="False"
                             ReadOnlyTemplate="{StaticResource MyTemplate}"
                             EditTemplate="{StaticResource MyTemplate}"
                             Grid.Row="1"
                             telerik:StyleManager.Theme="Office_Black"
                             CommandButtonsVisibility="Cancel,Commit,Edit" EditEnded="AccountInfo_EditEnded" >
             
        </telerik:RadDataForm>
    </Grid>
</Window>

Again, this coding solution works with no problems, but I'm trying learn as I go and am fairly certain my solution is likely not the best one.

Thanks, Mark

No answers yet. Maybe you can help?

Tags
DataForm
Asked by
visionharvest
Top achievements
Rank 1
Share this question
or