DataGrid using BindingList

微信扫一扫,分享到朋友圈

DataGrid using BindingList

I have a C# WPF project, using MongoDB as its backend. I was able to retrieve the data from the database to the DataGrid, and put some handlers. What I am trying to do now is give the ability to add data using textboxes. So that’s where my issue relates to, what I want is when I save an entity, I want the DataGrid to refresh, showing the new row that is added. I have tried the solutions that have read in some stackoverflow questions, like DataGrid.Items.Refresh(). Now, through reading some articles about ObservableCollections, I have learned that there is a better way doing this, by using BindingList. I was also able to retrieve data and list them to the DataGrid using BindingList, but still can’t refresh the collection whenever I save a new row. Here’s the code.

public partial class MainWindow : Window
    {

        MongoClient mongoClient { get; set; }
        MongoServer server { get; set; }
        MongoDatabase database { get; set; }
        MongoCollection 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 collection = database.GetCollection("faculty");
                var results = collection.FindAll();
                List resultList = results.ToList();
                BindingList resultBinding = new BindingList(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("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; }
    }

Try using

urBindingList.ResetBindings();

Spring依赖注入原理分析

上一篇

产品分析报告:「腾讯相册管家」,你的回忆,由我珍藏

下一篇

你也可能喜欢

DataGrid using BindingList

长按储存图像,分享给朋友