locked
Progress bar question RRS feed

  • Question

  • I am having troubles with progress bars. 

    This example below works but really is not the behavior I need.

    This form allows me to pick an appointment date with the first method, getting the people for that day, then the next two methods fill in the details for an appointment.

     

     

    Code Block

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Threading;

    namespace CheckApptCalendar

    {

    public partial class Form1 : Form

    {

    public string apptDate;

    public string database;

    public string database1; // one practice

    public string database2; // other practice

     

     

    public Form1()

    {

    InitializeComponent();

    dateTimePicker1.Value = (DateTime.Now);

    }

    public void button1_Click(object sender, EventArgs e)

    {

    smoothProgressBar1.Value = 0;

    ThreadStart threadstart = dowork;

    threadstart.BeginInvoke(null, null);

    }

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)

    {

    apptDate = dateTimePicker1.Value.ToString("yyyyMMdd");

    }

    public void smoothProgressBar1_Load(object sender, EventArgs e)

    {

    }

    public void UpdateProgressBar()

    {

    if (smoothProgressBar1.InvokeRequired)

    {

    MethodInvoker updateProgressBar = UpdateProgressBar;

    smoothProgressBar1.Invoke(updateProgressBar);

    }

    else

    {

    // i just put 5 in to see it work

    smoothProgressBar1.Value = smoothProgressBar1.Value + 5; 

    }

    }

    public void dowork()

    {

    ClassThatDoesWork classthatdoeswork1 = new ClassThatDoesWork();

    database1 = "database1";

    UpdateProgressBar();

    database2 = "database2";

    apptDate = dateTimePicker1.Value.ToString("yyyyMMdd");

    classthatdoeswork1.GetNumberOfAppointments(apptDate, database1, database2);

    UpdateProgressBar();

    classthatdoeswork1.FillResultsArraywithDetails(apptDate, database1);

    UpdateProgressBar();

    classthatdoeswork1.FillResultsArraywithDetails(apptDate, database2);

    UpdateProgressBar();

    ResultsView results = new ResultsView();

    results.DisplayResultsTable(classthatdoeswork1.resultsTable);

    results.ShowDialog();

    }

    }

    }

     

     

    What this sample above gives me is a rough progress bar as I go through each of the methods for ClassThatDoesWork.  This is a crude estimate however.

     

    What I cannot figure out is how to update the progress bar while I am doing work inside the individual methods of ClassThatDoesWork.  For example, when I FillResultsArraywithDetails, I would like to update the progress bar each time I iterate through an appointment (get name 1 - update progress by 5%, get name 2 - update progress to 10%, now get appt1 details - update to 15%, appt2 details complete - update to 20%, .... until I'm done.)   The percentages are arbitrary here, but it makes the example simpler.

     

    thanks!

    Monday, October 22, 2007 11:29 PM

Answers

  • You can handle the DataTable.RowChanged event to perform the process status while selecting data from the DataBase, or you can handle the SqlDataAdapter.RowUpdated event to perform the process status while updating data to the DataBase, I write a sample for perform the process status while selecting data from database, as:

    Code Block

    private void Form18_Load(object sender, EventArgs e)

            {

               

                SqlConnection conn = new SqlConnection(connstr);

                string sql = "SELECT * FROM [Order Details]";

                da = new SqlDataAdapter(sql, conn);

     

                dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);

            }

     

            string connstr = "Persist Security Info=False;Integrated Security=SSPI;" +

                    "Initial Catalog=Northwind;server=localhost";

            SqlDataAdapter da;

            DataTable dt = new DataTable();

     

            private void button1_Click(object sender, EventArgs e)

            {

                this.progressBar1.Maximum = getRowCount();

                this.progressBar1.Value = 0;

                dt.Clear();

                da.Fill(dt);

                this.dataGridView1.DataSource = dt;

            }

     

            void dt_RowChanged(object sender, DataRowChangeEventArgs e)

            {

                if (this.progressBar1.Value < dt.Rows.Count - 1)

                {

                    this.progressBar1.Value++;

                }

            }

     

            private int getRowCount()

            {

                int count = 0;

                SqlConnection conn = new SqlConnection(connstr);

                string sql = "SELECT COUNT(OrderID) FROM [Order Details]";

                SqlCommand cmd = new SqlCommand(sql,conn);

                try

                {

                    conn.Open();

                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())

                    {

                        count = (int)dr[0];

                    }

                }

                catch { }

                finally

                {

                    conn.Close();

                }

                return count;

            }

     

     



    Thursday, October 25, 2007 8:03 AM

All replies

  • You can do this through Event.

    Tuesday, October 23, 2007 3:10 AM
  • Great. Thanks.

     

    Could you provide a rough example of the event and where it would go?  Also even though form is public, I cannot see any form methods while I am in the classthatdoeswork  class.  However, I would be raising the event within this class?

     

    thank you.

     

    Tuesday, October 23, 2007 12:47 PM
  • I'm moving this posting to the WinForms forum.  Please reserve the C# Languages forum for questions specifically related the language of C#.  Thanks.

    Tuesday, October 23, 2007 1:36 PM
  • You can handle the DataTable.RowChanged event to perform the process status while selecting data from the DataBase, or you can handle the SqlDataAdapter.RowUpdated event to perform the process status while updating data to the DataBase, I write a sample for perform the process status while selecting data from database, as:

    Code Block

    private void Form18_Load(object sender, EventArgs e)

            {

               

                SqlConnection conn = new SqlConnection(connstr);

                string sql = "SELECT * FROM [Order Details]";

                da = new SqlDataAdapter(sql, conn);

     

                dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);

            }

     

            string connstr = "Persist Security Info=False;Integrated Security=SSPI;" +

                    "Initial Catalog=Northwind;server=localhost";

            SqlDataAdapter da;

            DataTable dt = new DataTable();

     

            private void button1_Click(object sender, EventArgs e)

            {

                this.progressBar1.Maximum = getRowCount();

                this.progressBar1.Value = 0;

                dt.Clear();

                da.Fill(dt);

                this.dataGridView1.DataSource = dt;

            }

     

            void dt_RowChanged(object sender, DataRowChangeEventArgs e)

            {

                if (this.progressBar1.Value < dt.Rows.Count - 1)

                {

                    this.progressBar1.Value++;

                }

            }

     

            private int getRowCount()

            {

                int count = 0;

                SqlConnection conn = new SqlConnection(connstr);

                string sql = "SELECT COUNT(OrderID) FROM [Order Details]";

                SqlCommand cmd = new SqlCommand(sql,conn);

                try

                {

                    conn.Open();

                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())

                    {

                        count = (int)dr[0];

                    }

                }

                catch { }

                finally

                {

                    conn.Close();

                }

                return count;

            }

     

     



    Thursday, October 25, 2007 8:03 AM
  • Great. thank you!  Not exactly what I was looking for, but I like it and this example gives me some good ideas.

     

    What if we fill the datatable,  dt, from another class which then winds up processing  the data in the table a bit.  How would I trigger the event from this different class?  I will be using a datareader but that is ok. 

     

    Friday, October 26, 2007 1:46 AM
  • Is the DataTable inside or outside your class? Anyway, I think the DataTable.RowChanged or SqlDataAdapter.RowUpdate events would work in the class scenario either.

    Friday, October 26, 2007 3:18 AM