This question is with reference to my old question here: Primary key in datagrid is always zero
这个问题与我以前的问题有关:datagrid中的主键总是0
In my previous question, i was working on pretty large database with many relationships, so i thought to give a try to and tested on this basic (very basic) project.
在我之前的问题中,我正在处理一个相当大的数据库,它有很多关系,所以我想尝试并测试这个基本的(非常基本的)项目。
What i did is i created simple basic wpf application. Added local database with only one table which has two columns.One is id column which is primary key and auto incremented.The other is simple varchar.I added that as data source and created grid from that data source.
我所做的是创建简单的基本wpf应用程序。添加了只有一个表、两个列的本地数据库。一个是id列,它是主键并自动递增。另一个是简单的varchar。我将其添加为数据源,并从该数据源创建网格。
here is my xaml:
这是我的xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication4" mc:Ignorable="d" x:Class="WpfApplication4.MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
<Window.Resources>
<CollectionViewSource x:Key="tablesViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Table}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource tablesViewSource}">
<DataGrid x:Name="tablesDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="47,24,70,95" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding name}" Header="name" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="Button" HorizontalAlignment="Left" Margin="221,266,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
The only two line of code is written by me. Rest is auto generated.Now i wanted to update my data from gridview, but the big problem is data is not updated.It keep adding zero.
只有两行代码是我写的。休息是自动生成的。现在我想从gridview更新我的数据,但是最大的问题是数据没有更新。它一直加零。
I added entire cs file here.
我在这里添加了整个cs文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private System.Data.Objects.ObjectQuery<Table> GetTablesQuery(Database1Entities database1Entities)
{
System.Data.Objects.ObjectQuery<WpfApplication4.Table> tablesQuery = database1Entities.Tables;
// Returns an ObjectQuery.
return tablesQuery;
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
WpfApplication4.Database1Entities database1Entities = new WpfApplication4.Database1Entities();
// Load data into Tables. You can modify this code as needed.
System.Windows.Data.CollectionViewSource tablesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tablesViewSource")));
System.Data.Objects.ObjectQuery<WpfApplication4.Table> tablesQuery = this.GetTablesQuery(database1Entities);
tablesViewSource.Source = tablesQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var context = new WpfApplication4.Database1Entities();
// Database1Entities db = new Database1Entities();
// db.SaveChanges();
context.SaveChanges();
}
}
} `
} '
I have started to doubt my basics. Please Help.
我开始怀疑自己的基本原则。请帮助。
2 个解决方案
#1
0
This works for me (adjust variable names):
适用于我(调整变量名):
using System.Data.Entity;
private Database1Entities _context = new Database1Entities ();
private void Window_Loaded(object sender, RoutedEventArgs e) {
System.Windows.Data.CollectionViewSource tableViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tableViewSource")));
_context.Table.Load();
tableViewSource.Source = _context.Table.Local;
}
private void Button_Click(object sender, RoutedEventArgs e) {
_context.SaveChanges();
tableDataGrid.Items.Refresh();
}
More info at: https://msdn.microsoft.com/en-us/data/jj574514.aspx
更多信息:https://msdn.microsoft.com/en-us/data/jj574514.aspx
#2
0
Try This:
试试这个:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var context = new WpfApplication4.Database1Entities();
var table = context.Set<YourTable>();
table.Add(new YourTable { Id = id, Name = "John Doe" });
context.SaveChanges();
}
#1
0
This works for me (adjust variable names):
适用于我(调整变量名):
using System.Data.Entity;
private Database1Entities _context = new Database1Entities ();
private void Window_Loaded(object sender, RoutedEventArgs e) {
System.Windows.Data.CollectionViewSource tableViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tableViewSource")));
_context.Table.Load();
tableViewSource.Source = _context.Table.Local;
}
private void Button_Click(object sender, RoutedEventArgs e) {
_context.SaveChanges();
tableDataGrid.Items.Refresh();
}
More info at: https://msdn.microsoft.com/en-us/data/jj574514.aspx
更多信息:https://msdn.microsoft.com/en-us/data/jj574514.aspx
#2
0
Try This:
试试这个:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var context = new WpfApplication4.Database1Entities();
var table = context.Set<YourTable>();
table.Add(new YourTable { Id = id, Name = "John Doe" });
context.SaveChanges();
}