【问题标题】:ProgressBar With Percentage In Label.Text In Foreach带有百分比的进度条在 Foreach 中的 Label.Text 中
【发布时间】:2019-02-27 04:26:04
【问题描述】:

我有这段代码,我有一个 ProgressBar,我想在标签中写下我的 progressBar 百分比。如何在 foreach 循环中执行此操作?这是我的代码:

       prg.Style = ProgressBarStyle.Continuous;
       con.Open();
       //insignificant sql and listbox operations...
       prg.Value = 0;
       prg.Maximum = myDataTable.Rows.Count;
       foreach (DataRow myRows5 in myDataTable.Rows)
       {
           dgv.Rows.Add(...);
           prg.Value++;
           Application.DoEvents();
           lbl.Text = "Loading... %" + Convert.ToString("I will put here of percentage...");
       }

注意:百分比形式为 int percent = ((prg.Value / prg.Maximum) * 100)

谢谢...

【问题讨论】:

    标签: c# datagridview datatable progress-bar


    【解决方案1】:

    你可以像这样显示百分比:

    lbl.Text = string.Format("Loading... {0:p0}", (prg.Value / (double)prg.Maximum));
    

    p0 格式化程序会将 0 到 1 之间的值转换为带 0 个小数位的百分比。

    如果您确实需要百分比进行其他计算,您几乎可以按照您的描述进行计算:

    int percent = (int)(prg.Value / (double)prg.Maximum);
    

    注意将其中一个值转换为double,以确保不使用整数除法。

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多