views:

1738

answers:

2

Hello!

I have followed this tutorial which allowed me to create a Silverlight Datagrid that pulled back data from an SQL Server Database. My next step is to be able to perform CRUD on this set of data (hopefully via the datagrid by simply editing the fields for update etc. and having this post back). I have been informed that the datagrid needs to be set to have a "two-way binding mode" - However I am unsure of how to do this since the Xaml for the datagrid is literally - (Since the datagrid was dropped onto the Page.xaml file from the toolbox and generated automatically).

I specifically want to continue using this ADO.Net Entity Framework implementation. Can anybody offer me some advice or point me in the right direction with some samples? I would greatly appreciate it.

Kind regards.

EDIT: If you wouldn't mind having a quick glance over the content of the tutorial I linked to so that you understand exactly what my setup is that would be a great help. I'm completely new to Silverlight and essentially just want to know how to perform CRUD on a database using ADO.Net Entity Framework and a Silverlight Datagrid.

A: 

Goober,

I am not sure I will be answering your question, but I will show you how to bind information to the datagrid from code, and setting that information to two-way binding.

First off, here is a link that discusses the difference between one-way, one-time and two-way bindings.

There are three ways you can change the data source after an edit took place in the data grid:

First You can manually set the columns in the XAML and indicate that the binding to each variable is 'two-way'.

<data:DataGrid Name="data" AutoGenerateColumns="False">
                <data:DataGrid.Columns>
                    <data:DataGridTextColumn Header="Test"
                                             Binding="{Binding test, Mode=Two-Way}"/>
                </data:DataGrid.Columns>
    </data:DataGrid>

Second You can do the same thing in the code behind the page.

rpdata.ItemsSource = info;
rpdata.Columns.Clear();    
DataGridTextColumn user = new DataGridTextColumn();
                    user.Header = "User";
                    user.Binding = new System.Windows.Data.Binding("User");
                    user.Binding.Mode = BindingMode.TwoWay;

rpdata.Columns.Add(user);

rpdata represents the datagrid.

Third

Instead of linking each column manually, you can add the 'CellEditEnded' event to the data grid as shown below.

and in the code for the event, you can add the following code:

xt.CommitEdit();

This updates the data source.

You will have to create a new contract to your service that takes the entities that have been updated by the data grid.

[OperationContract]
public void UpdateWork(List<Assumptions> updates)

Here my entity is called 'Assumptions'.

In that Operation Contract you add the following code.

//Create a new entity datacontext
Entities ds = new Entities();


//For each of the entities in the list
foreach (Assumptions update in updates)
{
  try
  {
    //In the datacontext find the entity with the same primary key as the updated one
    var test = from x in ds.Assumptions
               where x.ID.Equals(update.ID)
               select x;

    //Update the entity
    test.First() = update;
  }
  catch (Exception e)
  {
    //If the entity does not exist, add it to the context
    ds.AddToAssumptions(update);
  }
}
//Save the context
ds.SaveChanges();
Johannes
So, currently i'm using the ADO.Net entity framework as a middle-man between my database and the silverlight datagrid. What steps do I need to take to change this setup from being simply a way of retrieving and displaying my data, to being able to make changes and persist them back to the database? (I currently can't see where the code above would fit into my project since there is actually very little going on at the moment).
Goober
So do I have to define a new "<data:DataGridTextColumn>" for every editable column that I want the datagrid to contain?
Goober
Yes, although you could probably bind the datasource to the grid, and loop through rpdata.Columns and set the bindingmode to two-way (in the code). I have not tested this yet.
Johannes
Can't believe i'm asking this, feel like a complete noob (only recently switched to web following 3 years on winforms) - where does the foreach loop go? After the xt.commitedit(); in the DATAGRID_CellEditEnded event!?
Goober
Well, I reformatted my answer, maybe this will make things more clear
Johannes
A: 

After Much Googling, this tutorial proved to help me do exactly what I wanted to do and was a fantastic help!

Good luck to anyone who continues with this implementation.

:-D

Goober