1

在 WinForm 应用程序中,我有一个包含 GridView 的表单,我通过从数据库中收集数据来填充 GridView。我有一个包含 Loading.gif 图像的图片框。我想要的是在从数据库中检索数据期间查看 PictureBox。

我尝试了下面的代码,但它没有工作......

 private void Generate_Button_Click(object sender, EventArgs e)
 {              
      pictureBox1.Visible = true;      
      {    
            Generate_Button.Text = "Done";
            if (conn.State == ConnectionState.Closed)
                conn.Open();                
            radGridView1.Columns.Add(new GridViewTextBoxColumn("Account No."));
            radGridView1.Columns[0].Width = 85;
            bool DataAvailable = false;
            if (MainAccNo_TextBox.Text == "" && CurencyNo_TextBox.Text == "")
            {
                if (SeparateBy_DropDownList.Text == "4")
                {
                    for (int i = 1; i <= 9; i++)
                    {
                        for (int j = 0; j <= 9; j++)
                        {
                            for (int k = 0; k <= 9; k++)
                            {
                                for (int l = 0; l <= 9; l++)
                                {
                                    SqlCommand cmd_AccNo = new SqlCommand("Select distinct(AccNo) from JourTrans where AccNo like '" + i + "" + j + "" + k + "" + l + "%'", conn);
                                    SqlDataReader reader_AccNo = cmd_AccNo.ExecuteReader();      
                                    radGridView1.Rows.Add(i + "" + j + "" + k + "" + l);
                                    while (reader_AccNo.Read())
                                    {
                                        Accounts.Add(reader_AccNo["AccNo"].ToString());
                                        radGridView1.Rows.Add(reader_AccNo["AccNo"].ToString());
                                        DataAvailable = true;
                                    }
                                    reader_AccNo.Close();
                                    if (DataAvailable == true)
                                    {
                                        radGridView1.Rows.Add("");
                                        DataAvailable = false;
                                    }
                                    else
                                      radGridView1.Rows.RemoveAt(radGridView1.Rows.Count - 1);
                                }
                            }
                        }
                    }
                }  
            }
        }                
     pictureBox1.Visible = false;
  }    

http://soundfrost.org/ >下载 youtube 视频

4

1 回答 1

2

在另一个线程中检索并使用 Dispatcher 以便您可以从该线程访问您的控件:

pictureBox1.Visible = true;   
Dispatcher.BeginInvoke(new MethodInvoker(() => 
{    
    for (int i = 1; i <= 9; i++)
    {                                               
        //Here I am retrieving data from the database       
    } 
    pictureBox1.Visible = false; 
}));
于 2013-02-25T12:54:57.470 回答