C#-如何在DataGridView填充时使用ProgessBar

时间:2019-02-19 02:20:58

标签: c# datagridview datatable progress-bar

我有一个DataGridView,它有很多行。加载需要很长时间。我想在加载时添加一个ProgressBar。如何在代码中添加带有Manquee样式的ProgressBar。 (附言:我不知道我的DataGridView的行数,它每次都会更改。)

这是我的代码;

     conection.Open();
     string[] ss = listBox2.SelectedItem.ToString().Split(' ');
     int countss = ss.Count();
     for (int mi = 0; mi < countss; mi++)
     {
         string mq = "select c1, c2, c3 from myTableName WHERE c1='" + ss[mi] + "' OR c2='" + ss[mi] + "'";
         SqlDataAdapter s = new SqlDataAdapter(mq, connection);
         DataTable d = new DataTable();
         s.Fill(d);
         foreach (DataRow r in d.Rows)
         {
             dgv1.Rows.Add(r["c1"].ToString() + " " + r["c2"].ToString(), r["c3"]);
         }
     }
     //Some insignificant operations...
     for (int mi2 = 0; mi2 < cmn; mi2++)
     {
         string mq2 = "select c1, c2, c3 from myTableName WHERE c1='" + cmn[mi2] + "' OR c2='" + cmn[mi2] + "'";
         SqlDataAdapter s2 = new SqlDataAdapter(mq2, conection);
         DataTable d2 = new DataTable();
         s2.Fill(d2);
         foreach (DataRow r2 in d2.Rows)
         {
             dgv2.Rows.Add(r2["c1"].ToString() + " " + r2["c2"].ToString(), r2["c3"]);
         }
     }
     coneciton.Close();

1 个答案:

答案 0 :(得分:0)

private void button1_Click(object sender, EventArgs e)
    {

        DataTable g = new DataTable();
        int rowCount = g.Rows.Count;
        Progress_tracker pForm = new Progress_tracker();
        pForm.Show();
        int rCount =0;
        foreach (DataRow r in g.Rows)
        {
            //do somethin

            pForm.progressBar1.Value = 100*rCount++ / rowCount;
        }
    }

这是有关如何更新的代码示例

private void InitializeComponent()
    {
        this.progressBar1 = new System.Windows.Forms.ProgressBar();
        this.SuspendLayout();
        // 
        // progressBar1
        // 
        this.progressBar1.Location = new System.Drawing.Point(37, 64);
        this.progressBar1.Name = "progressBar1";
        this.progressBar1.Size = new System.Drawing.Size(354, 23);
        this.progressBar1.TabIndex = 0;
        // 
        // Progress_tracker
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(530, 154);
        this.Controls.Add(this.progressBar1);
        this.Name = "Progress_tracker";
        this.Text = "Progress_tracker";
        this.ResumeLayout(false);

    }

这是Progresstracker窗口的设计器代码,该窗口只有一个控件和进度条,请记住保持其修改器为公开状态,以便您可以在类外(即通过主窗体)访问它