如何在WPF中更新我的数据集 [英] How can I update my dataset in WPF
本文介绍了如何在WPF中更新我的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have this problem when I put the Binding Mode to TwoWay the DataGrid won't show. When I leave the Binding Mode as it is on default, the DataGrid will apear as strings, and I cannot find the problem. In XAML I have 3 more buttons: Load(that loads the table), Update and Cancel(that cancel all the changes and reloads the DataGrid directly from ObservableCollection.
这是我的XAML DataGrid行:
Here is my XAML DataGrid line:
<DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Canvas.Left="10" Canvas.Top="10" AlternatingRowBackground="LightGreen" Height="245" Width="500" ItemsSource="{Binding Userss.UserCol, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding RelativeSource={RelativeSource Self}}"/>
我有一个用户类,我创建了我的ObservableCollection,我存储了我的SQLite数据库中的数据。
I have a Userss Class where i creat my ObservableCollection where i store the data from my SQLite database.
<pre lang="C#">public class Userss : INotifyPropertyChanged
{
public static SQLiteConnection m_dd = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
private static ObservableCollection<userss> userCol = new ObservableCollection<userss>();
public ObservableCollection<userss> UserCol
{
get { return userCol; }
set
{
userCol = value;
RaisePropertyChanged();
}
}
public int Id { get; set; }
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged();
}
}
private Sex _sex;
public Sex Sex
{
get { return _sex; }
set
{
_sex = value;
RaisePropertyChanged();
}
}
private Stations _station;
public Stations Station
{
get { return _station; }
set
{
_station = value;
RaisePropertyChanged();
}
}
private Jobs _job;
public Jobs Job
{
get { return _job; }
set
{
_job = value;
RaisePropertyChanged();
}
}
private DateTime _date;
public DateTime Date
{
get { return _date; }
set
{
_date = value;
RaisePropertyChanged();
}
}
public static ObservableCollection<Userss> GetValues()
{
m_dd.Open();
string sql = "select * from user";
userCol.Clear();
SQLiteCommand cmd = new SQLiteCommand(sql, m_dd);
SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string sex1 = reader["sex"].ToString();
string station1 = reader["station"].ToString();
string job1 = reader["job"].ToString();
string data1 = reader["date"].ToString();
userCol.Add(new Userss()
{
Id = Convert.ToInt32(reader["id"]),
Name = reader["name"].ToString(),
Sex = (Sex)Enum.Parse(typeof(Sex), sex1),
Station = (Stations)Enum.Parse(typeof(Stations), station1),
Job = (Jobs)Enum.Parse(typeof(Jobs), job1),
Date = Convert.ToDateTime(data1)
});
}
m_dd.Close();
return userCol;
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string caller = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}
}
public enum Sex
{
Male,
Female
}
public enum Jobs
{
Programmer,
Designer,
Manager,
CTO,
CEO,
}
public enum Stations
{
Desktop,
Laptop,
Tablet
}
And here is my implementation for my MainWindow:
public partial class MainWindow : Window
{
public SQLiteConnection m_db = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
SQLiteDataAdapter adap;
DataSet ds;
DataTable dt;
SQLiteCommandBuilder cmdbl;
string Query;
public MainWindow()
{
InitializeComponent();
}
private void LoadButton_Click(object sender, RoutedEventArgs e)
{
dataGrid.ItemsSource = Userss.GetValues();
}
private void Update_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Are you sure you want to make those changes?", "Please confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
try
{
m_db.Open();
cmdbl = new SQLiteCommandBuilder(adap);
Query = "Select * from user";
adap = new SQLiteDataAdapter(Query, m_db);
ds = new DataSet();
adap.Fill(ds, "Users");
dt = ds.Tables[0];
ds.Tables[0].AcceptChanges();
adap.Update(ds, "Users");
dt.AcceptChanges();
adap.Update(dt);
dataGrid.Items.Refresh();
m_db.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
this.dataGrid.CancelEdit();
}
private void CancelClick(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Are you sure you want to cancel those changes?", "Please confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
dataGrid.ItemsSource = Userss.GetValues();
}
else
this.dataGrid.CancelEdit();
}
}
}
Btw I work in WPF.
Hope someone can help me. Thanks.
我尝试过:
What I have tried:
I've tryed to debug and see what is happening with the dataset and data table and I saw that they don't change at all.
推荐答案
0)您需要绑定一个公共属性。
公共用户用户{get;组; }
你可以在构造函数中设置DataContext:
this.DataContext = this; < br $> b $ b
1)为什么要在XAML和代码隐藏中设置ItemsSource?
2 )划分您的代码。将数据库查询对象和代码放入方法中。实际上,我会在其中创建一个包含数据库内容的静态类,这样就不会弄乱你的UI代码。
0) You need a public property to bind to.
public Userss Users { get; set; }
And you can set the DataContext in the constructor:
this.DataContext=this;
1) Why are you setting the ItemsSource in both the XAML and the code-behind?
2) Compartmentalize your code. Put the database query objects and code into a method. In fact, I would create a static class with my database stuff in it, and that way, it's not cluttering up your UI code.
这篇关于如何在WPF中更新我的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文