I want to update progress bar after a method which is present in another class is called. In my case am reading data from a excel and storing it in datatable for further operations as the excel file is large it takes some time. I want to update the progress of reading and stroing data on UI.
Following is my UI(xaml) code:
<Window x:Class="WpfTutorialSamples.Misc_controls.ProgressBarIndeterminateSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ProgressBarIndeterminateSample" Height="100" Width="300">
<Grid Margin="20">
<button click="btn_submit_click">Run</button>
<ProgressBar Minimum="0" Maximum="100" Name="pbStatus" IsIndeterminate="True" />
</Grid>
</Window>
Following is my xaml.cs code:
namespace progress_bar
{
public partial class MainWindow : Window
{
public static string path = @"c:\sample.xlsx";
public MainWindow()
{
InitializeComponent();
}
private void btn_submit_click(object sender, RoutedEventArgs e)
{
Read_file R1 = new Read_file();
var DT = R1.read(path);
}
}
class Read_file
{
public DataTable dt = new DataTable("dt"); // creating datatable
string Name = string.Empty;
dt.Columns.Add("Name", typeof(String)); // adding "Name" column to datatable
public DataTable read(string path)
{
//Performed some operation such as opening, etc
//Applied Autofilter on excel file as I have to select specific data from excel file
//filtered_data is Excel Range of Filtered data
///using this loop to iterate through filtered data and store it Datatable
foreach(Excel.Range area in filtered_data.Areas)
{
foreach(Excel.Range row in area.Rows)
{
int index = row.Row // selecting row index
////select each row from filtered data and saving it to Datatable.
Name = Convert.ToString((xlsheet.cells[index,1] as Excel.Range).Text);
df.Rows.Add(Name);
//// I want to display the progress of selectig and saving data to UI
}
}
}
}
}
I want to update progress bar according to the execution in foreach loop inside Read method. On internet I read about the BackgroundWorker but I am very confused about where and how to use BackgroundWorker methods. I am still new to c# and wpf.