我想使用后台工作程序从表更新datagridview。那可能吗 ?并且还怀疑它是否会降低我的应用程序性能
是的你可以。下面是一个示例,其中主UI空间中的DataGridView在backgroundWorker完成后更新。
BackgroundWorker backgroundWorker;
public MainWindow()
{
InitializeComponent();
// initializing GridView with data
this.dataGrid.ItemsSource = new people[] {
new people { Name = "John", LastName="Doe", Age = 40 } };
// adding a button click handler
this.btnAddPeople.Click += BtnAddPeople_Click;
backgroundWorker = backgroundWorker ?? new BackgroundWorker();
backgroundWorker.DoWork += BackgroundWorker_DoWork;
backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
}
private void BtnAddPeople_Click(object sender, RoutedEventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
var newList = this.dataGrid.ItemsSource.Cast<people>().Union(
new[] {
new people { Name = "Asher", LastName = "Richard", Age = 26 },
new people { Name = "Joyce", LastName = "Herrera", Age = 37 }
}
);
this.dataGrid.ItemsSource = newList;
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1000);
}
class people
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}