DataGrid使用BindingList刷新

时间:2015-02-27 11:23:23

标签: c# wpf mongodb data-binding datagrid

我有一个C#WPF项目,使用MongoDB作为后端。我能够从数据库中检索数据到DataGrid,并放置一些处理程序。我现在要做的是提供使用文本框添加数据的功能。这就是我的问题所涉及的,我想要的是当我保存一个实体时,我希望DataGrid刷新,显示添加的新行。我已经尝试过在一些stackoverflow问题中读过的解决方案,比如DataGrid.Items.Refresh()。现在,通过阅读一些关于ObservableCollections的文章,我了解到有一种更好的方法,通过使用BindingList。我还能够使用BindingList检索数据并将它们列出到DataGrid,但每当我保存新行时仍然无法刷新集合。这是代码。

public partial class MainWindow : Window
    {

        MongoClient mongoClient { get; set; }
        MongoServer server { get; set; }
        MongoDatabase database { get; set; }
        MongoCollection<facultyData> collection { get; set; }

        public MainWindow()
        {
            InitializeComponent();
        }

        public void Window_Loaded(object sender, RoutedEventArgs e)
        {

                MongoClient mongoClient = new MongoClient();
                MongoServer server = mongoClient.GetServer();
                MongoDatabase database = server.GetDatabase("facultyDataAndSchedule");
                MongoCollection<facultyData> collection = database.GetCollection<facultyData>("faculty");
                var results = collection.FindAll();
                List<facultyData> resultList = results.ToList<facultyData>();
                BindingList<facultyData> resultBinding = new BindingList<facultyData>(resultList);
                resultBinding.RaiseListChangedEvents = true;

                if (resultBinding.Count() > 0)
                {
                    Binding bind = new Binding(); //create a new binding to be used on the wpf 
                    facultyDataGrid.DataContext = resultBinding; //sets the data binding for the control
                    facultyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, bind); //syncs the data
                    facultyID_Textbox.DataContext = resultBinding;
                    facultyID_Textbox.SetBinding(DataGrid.ItemsSourceProperty, bind);
                    lastName_TextBox.DataContext = resultBinding;
                    lastName_TextBox.SetBinding(DataGrid.ItemsSourceProperty, bind);
                }          
        }
        void listOfParts_ListChanged(object sender, ListChangedEventArgs e)
        {
            MessageBox.Show(e.ListChangedType.ToString());
        }
        public void addData_Click(object sender, RoutedEventArgs e)
        {
            var connectionString = "mongodb://localhost";
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            var database = server.GetDatabase("facultyDataAndSchedule");
            var collection = database.GetCollection<facultyData>("faculty");



                var entity = new facultyData { facultyID = facultyID_Textbox.Text.ToString(), 
                    age= Int32.Parse(age_TextBox.Text), 
                    firstName= firstName_TextBox.Text.ToString(),
                    lastName = lastName_TextBox.Text.ToString(),
                    middleName = middleName_TextBox.Text.ToString(),
                    dateOfBirth="11/11/2011", 
                    program= "progra", rank="gegs", services="gegsg", status="geh", yearsOfTeachingO=1, yearsOfTeachingS=1};
                collection.Insert(entity);


        }

        private void refreshButton_Click(object sender, RoutedEventArgs e)
        {
            facultyDataGrid.ItemsSource = null;
            facultyDataGrid.Items.Clear();
            facultyDataGrid.ItemsSource="{Binding}";


        }

    }

    class facultyData
    {
        public ObjectId _id { get; set; }
        public string facultyID { get; set; }
        public string acadYear { get; set; }
        public string program { get; set; }
        public string lastName { get; set; }
        public string firstName { get; set; }
        public string middleName { get; set; }
        public string dateOfBirth { get; set; }
        public int age { get; set; }
        public string rank { get; set; }
        public int yearsOfTeachingS { get; set; }
        public int yearsOfTeachingO { get; set; }
        public string status { get; set; }
        public string services { get; set; }
    }

2 个答案:

答案 0 :(得分:1)

尝试使用

urBindingList.ResetBindings();

答案 1 :(得分:1)

现在解决了,感谢Microsoft DN,使用myBindingList.ResetBindings();,但必须重写DataGrid。

resultBinding.ResetBindings();

        if (resultBinding.Count() > 0)
        {
            Binding bind = new Binding(); //create a new binding to be used on the wpf 
            facultyDataGrid.DataContext = resultBinding; //sets the data binding for the control
            facultyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, bind); //syncs the data

        }