silverlight:Silverlight 2 - Simple Editing of Web Service Data in a DataGrid

  Somebody asked how you use the DataGrid in Silverlight 2 in order to load data from a web service, mody it and submit it back so I thought I'd spend 5 minutes experimenting with that.

  I wrote a very simple web service with WCF;

[DataContract]
public Person
{
 [DataMember]
 public Id { get; ; }
 [DataMember]
 public FirstName { get; ; }
 [DataMember]
 public LastName { get; ; }
 [DataMember]
 public Age { get; ; }
}
[ServiceContract]
public erface IPeopleService
{
 [OperationContract]
 List<Person> GetPeople;
 [OperationContract]
 void UpdatePeople(List<Person> inserts, List<Person> updates,
  List<Person> deletes);
}
  and implemented it in the simplest possible way ( no thread-safety, no concurrency );

public PeopleService : IPeopleService
{
  PeopleService
 {
  people = Dictionary<, Person>;
  people.Add(1, Person { Id = 1, FirstName = "Mike", LastName = "Taulty", Age = 18 });
  people.Add(2, Person { Id = 2, FirstName = "Mike", LastName = "Ormond", Age = 78 });
  people.Add(3, Person { Id = 3, FirstName = "Daniel", LastName = "Moth", Age = 101});
 }
 public List<Person> GetPeople
 {
   (people.Values.ToList);
 }
 public void UpdatePeople(List<Person> inserts, List<Person> updates, List<Person> deletes)
 {
   (inserts != null)
  {
   foreach (Person p in inserts)
   {
    p.Id = nextId;
    people.Add(p.Id, p);
   }
  }
   (deletes != null)
  {
   foreach (Person p in deletes)
   {
    people.Remove(p.Id);
   }
  }
   (updates != null)
  {
   foreach (Person p in updates)
   {
    Person updateEntry = people[p.Id];
    updateEntry.FirstName = p.FirstName;
    updateEntry.LastName = p.LastName;
    updateEntry.Age = p.Age;
   }
  }
 }
 private Dictionary<, Person> people;
 private nextId = 4;
}
  and I made sure that the web.config file that had been edited by the tool when I inserted the WCF service had basicHttpBinding rather than the wsHttpBinding you get by default.

  From there, I added in a Silverlight project and created a simple UI.

Silverlight 2 - Simple Editing of Web Service Data in a DataGrid

  It's just a DataGrid and a couple of buttons. I added a service reference to my WCF service in order to get a proxy to use to call the service asynchronously.

  In order to populate the DataGrid, I make a call to my webservice, get the data, convert the results o an ObservableCollection<T> where T is a local that I called DataRow;

 public enum RowState
 {
  Clean,
  Modied,
  Inserted
 }
 public DataRow
 {
  public DataRow
  {
   State = RowState.Clean;
  }
  public Id { get; ; }
  public FirstName { get; ; }
  public LastName { get; ; }
  public Age { get; ; }
  public RowState State { get; ; }
 }
  and then I just up the UI so that it data binds 4 text box columns to the properties _disibledevent= Visibility.Collapsed;
   foreach (var person in e.Result)
   {
    data.Add(PersonToDataRow(person));
   }
  }
  private ObservableCollection<DataRow> data;
  private List<DataRow> deletions;
 }  Note that at some po I expect that you'll have to use Dispatcher.Invoke to get back to the UI thread when these asynchronous calls complete but in the current beta it seems that you don't have to do that. Maybe it'd be better practise to put it in anyway to future proof the code a bit but, as I say, that code is pretty hacky anyway :-)



  I've uploaded the whole project here in it might help anybody.



Tags:  silverlight2 silverlight.2.0 silverlight是什么 silverlight

延伸阅读

最新评论

发表评论