Implement Progressbar

Asked

Viewed 260 times

0

 //cria conexao com o DB
        OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Casa\Desktop\Inventario.mdb");

        //comando SQL
        OleDbCommand aCommand = new OleDbCommand("INSERT INTO Inventario (CodProd, Qtd, ValorUnit, ValorMerc)" + "Values (@CodProd,Qtd, @ValorUnit, @ValorMerc)", aConnection);

        aConnection.Open();
        try
        {
            //percorro o DataGridView
            for (int i = 0; i < vGrade.Rows.Count - 1; i++)
            {
                //limpo os parâmetros
                aCommand.Parameters.Clear();
                //crio os parâmetro do comando
                //e passo as linhas do dgvClientes para eles
                //onde a célula indica a coluna do dgv
                aCommand.Parameters.AddWithValue("@CodProd",
                     vGrade.Rows[i].Cells[0].Value);
                aCommand.Parameters.AddWithValue("@Qtd",
                    vGrade.Rows[i].Cells[1].Value);
                aCommand.Parameters.AddWithValue("@ValorUnit",
                    vGrade.Rows[i].Cells[2].Value);
                aCommand.Parameters.AddWithValue("@ValorMerc",
                   vGrade.Rows[i].Cells[3].Value);
                //executo o comando
                aCommand.ExecuteNonQuery();
            }

            MessageBox.Show("Concluído");

            //fecha a conexao 
            aConnection.Close();
        }


        catch (FormatException)
        {
            MessageBox.Show("Houve um erro");

        }
    }

How can I implement the progressbar in the above code?

I was using the code below to perform a similar task ( display the progress of the action), but I don’t know how to implement in the above code, what should I do?

 try
     {
         aConnection.Open();
         //cria objeto datareader  para conexao com a tabela
         OleDbDataReader aReader = aCommand.ExecuteReader();

         while (aReader.Read())
         {
             progressBar1.Increment(1);
             label1.Text = progressBar1.Value.ToString() + "%";
         }

         //fecha o reader
         aReader.Close();
         //fecha a conexao 
         aConnection.Close();
     }

     finally { }
 }*/
  • WPF or Windows Forms?

2 answers

1

See if the code helps you

//cria conexao com o DB
    OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Casa\Desktop\Inventario.mdb");

    //comando SQL
    OleDbCommand aCommand = new OleDbCommand("INSERT INTO Inventario (CodProd, Qtd, ValorUnit, ValorMerc)" + "Values (@CodProd,Qtd, @ValorUnit, @ValorMerc)", aConnection);

    aConnection.Open();
    try
    {
        //define o valor maximo da toolbar com o total de iterações
        progressBar1.Maximum = vGrade.Rows.Count;

        progressBar1.Minimum = 0;

        var percent = 0.0;

        //percorro o DataGridView
        for (int i = 0; i < vGrade.Rows.Count - 1; i++)
        {
            //limpo os parâmetros
            aCommand.Parameters.Clear();
            //crio os parâmetro do comando
            //e passo as linhas do dgvClientes para eles
            //onde a célula indica a coluna do dgv
            aCommand.Parameters.AddWithValue("@CodProd",
                 vGrade.Rows[i].Cells[0].Value);
            aCommand.Parameters.AddWithValue("@Qtd",
                vGrade.Rows[i].Cells[1].Value);
            aCommand.Parameters.AddWithValue("@ValorUnit",
                vGrade.Rows[i].Cells[2].Value);
            aCommand.Parameters.AddWithValue("@ValorMerc",
               vGrade.Rows[i].Cells[3].Value);
            //executo o comando
            aCommand.ExecuteNonQuery();

           progressBar1.Increment(1);

           //verifica a porcentagem de completude para exibir no label
          percent = ((double)progressBar1.Value / (double)progressBar1.Maximum) * 100;

          //existe o valor no label
          label1.Text = string.Concat(percent.ToString(),"%");
        }

        MessageBox.Show("Concluído");

        //fecha a conexao 
        aConnection.Close();
    }


    catch (FormatException)
    {
        MessageBox.Show("Houve um erro");

    }
}

0

Insert this code snippet below the line aCommand.Executenonquery();

...

      //Calcula a porcentagem que i representa do total
      int percent = (int)(100 * (i+1)/vGrade.Rows.Count);

      // atualiza a progressbar
      progressbar1.value = percent;
   ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.