C# Can I add values to a listbox with a backgroundwork thread?

后端 未结 6 1309
盖世英雄少女心
盖世英雄少女心 2021-01-03 01:27

I want my background worker to add items to a list box, it appears to do so when debugging but the listbox doesn\'t show the values. I suspect this is something to do with a

6条回答
  •  旧时难觅i
    2021-01-03 02:03

    if you are trying to update a database. From a listbox i would suggest creating a dataset.

    for instance, if your doing something for each item in a database. Copy the database dataset, by creating new dataset and declaring by mainDataset.

    for example: // the gridview dataset is dataset1

    BackgroundWorker_DoWork(object sender, DoWorkArgs e)
    {
         Dataset dataset2 = dataset1;
         foreach(DataGridViewRow row in GridView)
         {
             //do some work
             dataset2.Main.AddMainRow(values to add);
             dataset2.AcceptChanges();
         }
    }
    
    
    BackgroundWorker_WorkCompleted(object sender, DoWorkArgs e)
    {
        //Forces UI thread to valitdate dataset
        dataset2.update();
    
        // Sets file Path
        string FilePath = "Some Path to file";
    
        dataset2.writexml(FilePath, XmlWriteOptions.WriteSchema);
    
        //if you use xml to fill your dataset filepath to write should equal path to dataset1 xml
        dataset1.Refresh();
    }
    

提交回复
热议问题