473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Updating Changes Made in silverlight datagrid to database

I am using a datagrid in my Silverlight App. I have binded columns of that datagrid to an observable collection which is being filled by data from WCF service at Page Load. I want that whatever changes i make to datagrid at runtime, i.e modify something or add a new row, or delete a row. it should get updated permanently to the database on click of Save button. How can i achieve it.

My Silverlight Page XAML.

Expand|Select|Wrap|Line Numbers
  1. <controls:ChildWindow xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="slLHWProduct.PartsWindow"
  2.            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.            xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
  5.                       xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
  6.                       Width="581" Height="321" Loaded="PartsWindow_OnLoaded"
  7.            Title="Parts">
  8.     <Grid x:Name="LayoutRoot" Margin="2">
  9.         <StackPanel Orientation="Vertical">
  10.             <StackPanel  Orientation="Horizontal">
  11.                 <Button x:Name="btnAddBlankRow"
  12.                         Width="Auto"
  13.                         Height="22"
  14.                         Margin="0,0,5,0"
  15.                         HorizontalAlignment="Left"
  16.                         VerticalAlignment="Bottom"
  17.                         Click="btnAddBlankRow_Click"
  18.                         FontSize="12">
  19.                     <Button.Content>
  20.                         <TextBlock Text="Add Blank Row" />
  21.                     </Button.Content>
  22.                 </Button>
  23.                 <Button x:Name="btnCopySelectedRows"
  24.                         Width="Auto"
  25.                         Height="22"
  26.                         Margin="0,0,5,0"
  27.                         HorizontalAlignment="Left"
  28.                         VerticalAlignment="Bottom"
  29.                         Click="btnCopySelectedRows_Click"
  30.                         FontSize="12">
  31.                     <Button.Content>
  32.                         <TextBlock Text="Copy Selected Rows" />
  33.                     </Button.Content>
  34.                 </Button>
  35.                 <Button x:Name="btnDeleteSelectedRows"
  36.                         Width="Auto"
  37.                         Height="22"
  38.                         Margin="0,0,5,0"
  39.                         HorizontalAlignment="Left"
  40.                         VerticalAlignment="Bottom"
  41.                         Click="btnDeleteSelectedRows_Click"
  42.                         FontSize="12">
  43.                     <Button.Content>
  44.                         <TextBlock Text="Delete Selected Rows" />
  45.                     </Button.Content>
  46.                 </Button>
  47.                 <Button x:Name="btnSaveChanges"
  48.                         Width="Auto"
  49.                         Height="22"
  50.                         Margin="0,0,5,0"
  51.                         HorizontalAlignment="Left"
  52.                         VerticalAlignment="Bottom"
  53.                         Click="btnSaveChanges_Click"
  54.                         FontSize="12">
  55.                     <Button.Content>
  56.                         <TextBlock Text="Save Changes" />
  57.                     </Button.Content>
  58.                 </Button>
  59.                 <Button x:Name="btnExport"
  60.                         Width="Auto"
  61.                         Height="22"
  62.                         Margin="0,0,5,0"
  63.                         HorizontalAlignment="Left"
  64.                         VerticalAlignment="Bottom"
  65.                         Click="btnExport_Click"
  66.                         FontSize="12">
  67.                     <Button.Content>
  68.                         <TextBlock Text="Export" />
  69.                     </Button.Content>
  70.                 </Button>
  71.             </StackPanel>
  72.             <StackPanel>
  73.                 <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
  74.                     <sdk:DataGrid 
  75.  
  76.             x:Name="dgPartsData"
  77.             AutoGenerateColumns="False"
  78.                          ScrollViewer.VerticalScrollBarVisibility="Visible"
  79.                         ScrollViewer.HorizontalScrollBarVisibility="Visible"
  80.             RowHeaderWidth="30"
  81.             TabNavigation="Cycle"
  82.             FontSize="12"
  83.             SelectionMode="Single"
  84.             HeadersVisibility="All"
  85.             CanUserResizeColumns="True"
  86.             CanUserSortColumns="True"
  87.             CellEditEnded="DgPartsData_OnCellEditEnded"
  88.             CurrentCellChanged="DgPartsData_OnCurrentCellChanged"
  89.                     HorizontalScrollBarVisibility="Auto"
  90.                     VerticalScrollBarVisibility="Auto"
  91.             >
  92.                         <sdk:DataGrid.Columns>
  93.                             <sdk:DataGridTemplateColumn Header="Name" SortMemberPath="Name">
  94.                                 <sdk:DataGridTemplateColumn.CellTemplate>
  95.                                     <DataTemplate>
  96.                                         <TextBlock Text="{Binding Name}" FontSize="12" Margin="2" Tag="{Binding PartId}"></TextBlock>
  97.                                     </DataTemplate>
  98.                                 </sdk:DataGridTemplateColumn.CellTemplate>
  99.                                 <sdk:DataGridTemplateColumn.CellEditingTemplate>
  100.                                     <DataTemplate>
  101.                                         <TextBox Text="{Binding Name,Mode=TwoWay}" FontSize="12" Margin="2" GotFocus="TextBox_GotFocus" LostFocus="TextBox_OnLostFocus"></TextBox>
  102.                                     </DataTemplate>
  103.                                 </sdk:DataGridTemplateColumn.CellEditingTemplate>
  104.                             </sdk:DataGridTemplateColumn>
  105.  
  106.                             <sdk:DataGridTemplateColumn Header="Description" SortMemberPath="Description">
  107.                                 <sdk:DataGridTemplateColumn.CellTemplate>
  108.                                     <DataTemplate>
  109.                                         <TextBlock Text="{Binding Description}" FontSize="12" Margin="2"></TextBlock>
  110.                                     </DataTemplate>
  111.                                 </sdk:DataGridTemplateColumn.CellTemplate>
  112.                                 <sdk:DataGridTemplateColumn.CellEditingTemplate>
  113.                                     <DataTemplate>
  114.                                         <TextBox Text="{Binding Description,Mode=TwoWay}" FontSize="12" Margin="2" GotFocus="TextBox_GotFocus"></TextBox>
  115.                                     </DataTemplate>
  116.                                 </sdk:DataGridTemplateColumn.CellEditingTemplate>
  117.                             </sdk:DataGridTemplateColumn>
  118.                             <sdk:DataGridTemplateColumn Header="InUse" SortMemberPath="InUse">
  119.                                 <sdk:DataGridTemplateColumn.CellTemplate>
  120.                                     <DataTemplate>
  121.                                         <TextBlock Text="{Binding InUse}" FontSize="12" Margin="2"></TextBlock>
  122.                                     </DataTemplate>
  123.                                 </sdk:DataGridTemplateColumn.CellTemplate>
  124.                             </sdk:DataGridTemplateColumn>
  125.                         </sdk:DataGrid.Columns>
  126.                     </sdk:DataGrid>
  127.                 </ScrollViewer>
  128.             </StackPanel>
  129.         </StackPanel>
  130.         <controlsToolkit:BusyIndicator x:Name="biGlobal" BusyContent="Retrieving/Updating data, please wait..." IsBusy="False" />
  131.     </Grid>
  132. </controls:ChildWindow>
  133.  
My C# Code.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Animation;
  13. using System.Windows.Shapes;
  14. using LinqToSqlClientHelper;
  15. using slLHWProduct.LHWSLAdminServices;
  16.  
  17. namespace slLHWProduct
  18. {
  19.     public partial class PartsWindow : ChildWindow
  20.     {
  21.         public PartsWindow()
  22.         {
  23.             InitializeComponent();
  24.             this._ProductId = 0;
  25.             biGlobal.IsBusy = true;
  26.         }
  27.         public class PartsData
  28.         {
  29.             public int PartId { get; set; }
  30.             public string Name { get; set; }
  31.             public string Description { get; set; }
  32.             public int InUse { get; set; }
  33.         }
  34.         #region variables
  35.  
  36.         private int _ProductId = 0;
  37.         public int ProductId
  38.         {
  39.             get { return _ProductId; }
  40.             set { _ProductId = value; }
  41.         }
  42.         public LHWSLAdminServicesClient client;
  43.         public ObservableCollection<PartsData> oc_Original;
  44.  
  45.         #endregion
  46.         private void PartsWindow_OnLoaded(object sender, RoutedEventArgs e)
  47.         {
  48.             client = new LHWSLAdminServicesClient();
  49.             oc_Original = new ObservableCollection<PartsData>();
  50.             if (_ProductId > 0)
  51.             {
  52.                 client.GetProductPartsAsync(_ProductId);
  53.                 client.GetProductPartsCompleted += new EventHandler<GetProductPartsCompletedEventArgs>(client_GetProductPartsCompleted);
  54.             }
  55.         }
  56.  
  57.         void client_GetProductPartsCompleted(object sender, GetProductPartsCompletedEventArgs e)
  58.         {
  59.             if (e.Result != null)
  60.             {
  61.                 foreach (var item in e.Result)
  62.                 {
  63.                     oc_Original.Add(new PartsData
  64.                         {
  65.                             PartId = item.PartID,
  66.                             Name = item.Name,
  67.                             Description = item.DESCRIPTION,
  68.                             InUse = item.InUse.Value
  69.                         });
  70.                 }
  71.                 dgPartsData.ItemsSource = oc_Original;
  72.                 biGlobal.IsBusy = false;
  73.             }
  74.         }
  75.         private void OKButton_Click(object sender, RoutedEventArgs e)
  76.         {
  77.             GC.Collect();
  78.             this.DialogResult = true;
  79.         }
  80.  
  81.         private void CancelButton_Click(object sender, RoutedEventArgs e)
  82.         {
  83.             GC.Collect();
  84.             this.DialogResult = false;
  85.         }
  86.  
  87.         private void DgPartsData_OnCurrentCellChanged(object sender, EventArgs e)
  88.         {
  89.             dgPartsData.BeginEdit();
  90.         }
  91.  
  92.         private void TextBox_GotFocus(object sender, RoutedEventArgs e)
  93.         {
  94.             ((TextBox)sender).SelectAll();
  95.         }
  96.  
  97.         private void DgPartsData_OnCellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
  98.         {
  99.             dgPartsData.CommitEdit();
  100.             dgPartsData.ItemsSource = oc_Original;
  101.         }
  102.  
  103.         private void TextBox_OnLostFocus(object sender, RoutedEventArgs e)
  104.         {
  105.             dgPartsData.CommitEdit();
  106.             dgPartsData.ItemsSource = oc_Original;
  107.         }
  108.  
  109.         private void btnAddBlankRow_Click(object sender, RoutedEventArgs e)
  110.         {
  111.             var item = oc_Original.Last().PartId;
  112.             oc_Original.Add(new PartsData
  113.                 {
  114.                     PartId = item+1,
  115.                     Name = "",
  116.                     Description = "",
  117.                     InUse = 0
  118.                 });
  119.             dgPartsData.ItemsSource = oc_Original;
  120.         }
  121.  
  122.         private void btnCopySelectedRows_Click(object sender, RoutedEventArgs e)
  123.         {
  124.  
  125.         }
  126.  
  127.         private void btnDeleteSelectedRows_Click(object sender, RoutedEventArgs e)
  128.         {
  129.             if (dgPartsData.SelectedItem != null)
  130.             {
  131.                 dgPartsData.CommitEdit();
  132.                 int PartID = Convert.ToInt32(((TextBlock)dgPartsData.Columns[0].GetCellContent(dgPartsData.SelectedItem)).Tag);
  133.                 MessageBox.Show(PartID.ToString());
  134.                 var item = oc_Original.FirstOrDefault(i => i.PartId == PartID);
  135.                 oc_Original.Remove(item);
  136.                 dgPartsData.ItemsSource = oc_Original;
  137.             }
  138.             else
  139.             {
  140.                 MessageBox.Show("Please select a part to delete");
  141.             }
  142.         }
  143.  
  144.         private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
  145.         {
  146.  
  147.         }
  148.  
  149.         private void btnExport_Click(object sender, RoutedEventArgs e)
  150.         {
  151.  
  152.         }
  153.     }
  154. }
My WCF Service.

Expand|Select|Wrap|Line Numbers
  1.    [OperationContract]
  2.     public List<LHWClasses.Admin.sp_LhwAdmin_GetProductPartsResult> GetProductParts(int ProductID)
  3.     {
  4.         LHWClasses.Admin.LHWSLAdminDataContext db=new LHWSLAdminDataContext();
  5.         var res = db.sp_LhwAdmin_GetProductParts(ProductID);
  6.         return res.ToList();
  7.     }
Database Sample is attached.

Please Help me to sort out this problem in best way .
Attached Images
File Type: png db.png (5.7 KB, 110 views)
Feb 21 '14 #1
0 1252

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chuck | last post by:
Good Morning: Has anyone in this newsgroup had experience with emailing changes made on a xml datagrid using ASPEmail? I currently have the table loading correctly with the correct data. When I...
6
by: ronwer | last post by:
Hello, The title doesn't completely cover the question I have, but it's a bit more complicated problem we have. We are using a database, based on Acces, but developed by a third party...
1
by: Martin Williams | last post by:
I have a main form with a bound combobox. I then have another form which is accessed from the main form, which I used to update the data that is bound to the combobox. I know how to update a...
1
by: eljainc | last post by:
Hello, I have a program written in C#.NET VS2005 that accesses a SQL database/data table through a dataset. It was decided that a valued defined as a smallint in SQL Server should be changed to...
6
by: mike11d11 | last post by:
I'm trying to create an application that will have multiple users working off a table on a SQL server. Since multi users will be updating different records at any given moment, how can i get those...
1
by: mankolele | last post by:
Hi all Is there someway to track changes made to data in a mysql database? Like someone changes a phone number in a record on a form -Is there a way to find out what changes were made to what...
6
by: lucyh3h | last post by:
Hi, In one of my pages, I use javascript (AJAX) to populate one mulitple select field based on user's click event on another control. I noticed that when I navigate back to this page by clicking...
3
by: =?Utf-8?B?QnJhbmRvbg==?= | last post by:
Hi, I have an aspx page that has the "include" code in it which includes another page that displays information. I also have an upload page that allows users to upload a simple html document...
5
by: vovan | last post by:
I have set of controls (Textboxes, checkboxes etc) along with the Grid on Windows Form. I use BindingSource to populate both Grid and the set of Controls. User selects the record in the grid and...
1
by: amitjaura | last post by:
Well i have a datagridview in my application and i want it to reflect any changes made in server database instantly with some trigger or so? I have a choice to use timer and get the desired results...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.