Как я могу обновить свой набор данных в 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:
<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}}"/>
У меня есть класс Userss, где я создаю свою ObservableCollection, где я храню данные из своей базы данных SQLite.
<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.
Что я уже пробовал:
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.