落花生,
我不知道我会回答你的问题,但我会告诉你如何从代码绑定信息给DataGrid
,并将该信息设置为双向绑定。
首先,这里是一个link,它讨论了单向,一次性和双向绑定之间的区别。
有三种方法可以更改数据源的编辑后,在数据网格中发生了:
首先 您可以手动设置列在XAML和指示给每个变量的绑定'双向'。
<data:DataGrid Name="data" AutoGenerateColumns="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Test"
Binding="{Binding test, Mode=Two-Way}"/>
</data:DataGrid.Columns>
</data:DataGrid>
二 你可以做同样的事情在后面的页面的代码。
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
代表数据网格。
第三
相反每一列手动联的,则可以将“CellEditEnded”事件添加到数据网格,如下所示。
和事件的代码,你可以添加以下代码:
xt.CommitEdit();
这将更新数据源。
您将不得不为您的服务创建一个新的契约,该契约采用已由数据网格更新的实体。
[OperationContract]
public void UpdateWork(List<Assumptions> updates)
这里我的实体被称为'假设'。
在该操作合同中添加以下代码。
//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();
因此,目前我使用ADO.Net实体框架作为我的数据库和silverlight数据网格之间的中间人。我需要采取哪些步骤来将此设置从简单的检索和显示数据的方式更改为能够进行更改并将其保留回数据库? (我目前无法看到上面的代码适合我的项目,因为此刻实际上很少发生)。 – Goober 2009-09-07 08:31:43
那么,我必须为每个我希望datagrid包含的可编辑列定义一个新的“”? – Goober 2009-09-07 08:44:47
是的,尽管您可能可能将数据源绑定到网格,并通过rpdata.Columns循环并将绑定模式设置为双向(在代码中)。我还没有测试过。 – Johannes 2009-09-07 10:49:20