c# -将我的数据表转换为ObservableCollection - WPF

本文关键字:ObservableCollection WPF 转换 数据表 我的 | 更新日期: 2023-09-27 18:07:54

我已经被告知我的数据表存储的信息填充组合框应该是可观察集合,使事情更容易。我使用的是WPF格式。

我试着看了多个答案,然而,我对这个很陌生,已经写了这么多的代码,我不确定如何将我写的所有东西转换成一个可观察的集合,或者如果它值得重写它将需要的量。

获取数据表数据的代码:

public partial class MainWindow : Window
    {
        DataTable dtNotes = new DataTable();
        DataTable dtTemplateNotes = new DataTable();
        DataTable dtReplaceVariables = new DataTable();
        public MainWindow()
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            DataContext = new TableList();
            //Setup connection to server
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = "PRETEND IP";
            builder.InitialCatalog = "DiscoverThePlanet";
            builder.UserID = "PRETEND USER";
            builder.Password = "PRETEND PASS";
            string connectionString = builder.ConnectionString;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmdNotes = new SqlCommand("SELECT NoteID, NoteName, Note FROM Notes", conn);
                SqlCommand cmdTemplateNotes = new SqlCommand("SELECT TemplateNoteID, TemplateNoteName, TemplateNote FROM TemplateNotes", conn);
                SqlCommand cmdReplaceVariables = new SqlCommand("SELECT ReplaceVariableID, ReplaceVariableName, ReplaceVariableNote FROM ReplaceVariables", conn);
                SqlDataReader readerNotes = cmdNotes.ExecuteReader();
                dtNotes.Columns.Add("NoteID", typeof(string));
                dtNotes.Columns.Add("NoteName", typeof(string));
                dtNotes.Columns.Add("Note", typeof(string));
                dtNotes.Load(readerNotes);
                SqlDataReader readerTemplateNotes = cmdTemplateNotes.ExecuteReader();
                dtTemplateNotes.Columns.Add("TemplateNoteID", typeof(string));
                dtTemplateNotes.Columns.Add("TemplateNoteName", typeof(string));
                dtTemplateNotes.Columns.Add("TemplateNote", typeof(string));
                dtTemplateNotes.Load(readerTemplateNotes);
                SqlDataReader readerReplaceVariables = cmdReplaceVariables.ExecuteReader();
                dtReplaceVariables.Columns.Add("ReplaceVariableID", typeof(string));
                dtReplaceVariables.Columns.Add("ReplaceVariableName", typeof(string));
                dtReplaceVariables.Columns.Add("ReplaceVariableNote", typeof(string));
                dtReplaceVariables.Load(readerReplaceVariables);
                // Temporary loop to see if the DataTable (dt) has any data?!?
                //foreach (DataRow thisRow in dt.Rows)
                //{
                //    MessageBox.Show(thisRow["NoteName"].ToString());
                //}

                // Define the columns BEFORE setting the item source
                noteNamesList.SelectedValuePath = "NoteID";
                noteNamesList.DisplayMemberPath = "NoteName";
                templateNoteNamesList.SelectedValuePath = "TemplateNoteID";
                templateNoteNamesList.DisplayMemberPath = "TemplateNoteName";
                replaceVariableNoteList.SelectedValuePath = "ReplaceVariableID";
                replaceVariableNoteList.DisplayMemberPath = "ReplaceVariableName";

                // Set the ItemSource to my fully loaded data table!
                noteNamesList.ItemsSource = dtNotes.DefaultView;
                templateNoteNamesList.ItemsSource = dtTemplateNotes.DefaultView;
                replaceVariableNoteList.ItemsSource = dtReplaceVariables.DefaultView;
                //DEBUG START
                //MessageBox.Show("Hello");
                //DEBUG END
                conn.Close();
            }
        }

当我提到我需要在命令运行后刷新这些数据表(更新表),然后为了用SQL数据库中的最新数据刷新组合框,我不得不重新启动我的应用程序时,这个主题就出现了。

这时有人建议我把这些数据表改成ObservableCollections

如果你需要更多的信息,请直接问,我是新手。

c# -将我的数据表转换为ObservableCollection - WPF

ObservableCollection只是某种类型的项的集合。
首先需要为item创建一个类:

public class MyNote
{
    public string Id { get; set; }
    public string Name { get; set; }
}

你可以这样写:

var notes = new ObservableCollection();
using(DataReader reader = cmdNotes.ExecuteReader())
{
    var ordinals = new 
    { 
        Id = reader.GetOrdinal("NoteID"),
        Name = reader.GetOrdinal("NoteName")
    }
    while(reader.Read() == true)
    {
        var temp = new Note();
        temp.Id = reader.GetString(ordinals.Id);
        temp.Name = reader.GetString(ordinals.Name);
        notes.Add(temp);
    }
}

noteNamesList.SelectedValuePath = "Id";
noteNamesList.DisplayMemberPath = "Name";
noteNamesList.ItemsSource = notes;

但在你的情况下,我认为更快的将是移动代码,从数据库中获取数据在单独的方法。然后调用你想用新数据更新窗口的方法