<DataGrid Margin="0,30,0,0" ItemsSource="{Binding GridData}" Name="dataGrid1" CanUserAddRows="False" AutoGenerateColumns="False" RowEditEnding="dataGrid1_RowEditEnding" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="ID" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding 食物名稱, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="食物名稱"/>
<DataGridTextColumn Binding="{Binding 熱量(Kcal), NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="100" Header="熱量(Kcal)"/>
<DataGridTextColumn Binding="{Binding 水分(g), NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="水分(g)"/>
<DataGridTextColumn Binding="{Binding 粗蛋白(g), NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="粗蛋白(g)"/>
<DataGridTextColumn Binding="{Binding 粗脂肪(g), NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="粗脂肪(g)"/>
<DataGridTextColumn Binding="{Binding 碳水化合物(g), NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="碳水化合物(g)"/>
</DataGrid.Columns>
</DataGrid>
private void dataGrid1_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
myConnection.Open();
using (OleDbCommand cmd =
new OleDbCommand("UPDATE 肉類 SET
[食物名稱]='" + ((DataRowView)e.Row.Item)["食物名稱"].ToString() +
"', [熱量(Kcal)]=" + ((DataRowView)e.Row.Item)["熱量(Kcal)"].ToString() +
", [水分(g)]=" + ((DataRowView)e.Row.Item)["水分(g)"].ToString() +
" WHERE Id='" + ((DataRowView)e.Row.Item)["ID"].ToString() + "';" , myConnection))
{
// Not allowed in Access database
//cmd.Parameters.AddWithValue("@Id", "12345");
//cmd.Parameters.AddWithValue("@食物名稱", "DDD");
//cmd.Parameters.AddWithValue("@熱量", "222");
//cmd.Parameters.AddWithValue("@水分", "333");
int rows = cmd.ExecuteNonQuery();
this.Title = rows.ToString() ;
//rows number of record got updated
}
myConnection.Close();
}