DataGrid使用BindingList刷新
DataGrid using BindingList refresh
我有一个 C# WPF 项目,使用 MongoDB 作为其后端。我能够将数据从数据库检索到 DataGrid,并放置一些处理程序。我现在要做的是提供使用文本框添加数据的能力。这就是我的问题所在,我想要的是当我保存一个实体时,我希望 DataGrid 刷新,显示添加的新行。我已经尝试了在一些 Whosebug 问题中读到的解决方案,比如 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; }
}
尝试使用
urBindingList.ResetBindings();
已解决,感谢微软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
}
我有一个 C# WPF 项目,使用 MongoDB 作为其后端。我能够将数据从数据库检索到 DataGrid,并放置一些处理程序。我现在要做的是提供使用文本框添加数据的能力。这就是我的问题所在,我想要的是当我保存一个实体时,我希望 DataGrid 刷新,显示添加的新行。我已经尝试了在一些 Whosebug 问题中读到的解决方案,比如 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; }
}
尝试使用
urBindingList.ResetBindings();
已解决,感谢微软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
}