I have a Silverlight DataGrid, for which the user can change values in. I also have a "Save" button. When the user开发者_如何转开发 clicks the "Save" button, I want only the rows (items) from the data grid that the user has changed to be saved. How can I accomplish this?
Well, if your DataGrid's ItemsSource property is bound to a collection of a class called MyClass, you could add a bool property to MyClass called IsModified. Then, in the other setters within that class, you could set IsModified to true. For example:
public class MyClass
{
public bool IsModified { get; set; }
private string _foo;
public string Foo
{
get { return _foo; }
set
{
_foo = value;
IsModified = true;
}
}
}
Then, you could use Linq to query the collection of items where IsModified is true (this code assumes items is the collection that is bound to your DataGrid):
List<MyClass> toSave = items.Where(x => x.IsModified).ToList();
Finally, use whatever save method you have to handle each item in toSave:
foreach (MyClass curr in toSave)
{
// Save "curr" here...
// Don't forget to reset IsModified
curr.IsModified = false;
}
Hope this helps.
加载中,请稍侯......
精彩评论