I want to inform the user while data is being read from an SQL database and I decided to create a form with a progressbar but it doesn't work - maybe because a thread is needed. I want to create the form programmatically
我想在从SQL数据库中读取数据时通知用户,我决定创建一个带有进度条的表单,但它不起作用 - 可能是因为需要一个线程。我想以编程方式创建表单
ProgressBar pb = new ProgressBar();
pb.MarqueeAnimationSpeed = 30;
pb.Style = ProgressBarStyle.Marquee;
pb.Dock = DockStyle.Fill;
progressForm.ClientSize = new Size(200, 50);
progressForm.FormBorderStyle = FormBorderStyle.FixedDialog;
progressForm.StartPosition = FormStartPosition.CenterScreen;
progressForm.Controls.Add(pb);
progressForm.ControlBox = false;
progressForm.TopMost = true;
progressForm.Show();
//do data processes here (all queries and executes)
progressForm.close();
How do I modify the code above to achieve my stated goals?
如何修改上面的代码以实现我的既定目标?
edit: Btw, I want to use this progressbar form in every data functions in my project. For example: fillGrid, runQuery..
编辑:顺便说一下,我想在项目的每个数据函数中使用这个进度条表单。例如:fillGrid,runQuery ..
@Will thank you very much for your answers. I meant how can I use a function of class for example my gridFill function is in that connection class:
@Will非常感谢你的回答。我的意思是我如何使用类的函数,例如我的gridFill函数在该连接类中:
class ConnectionClass
{
public static SqlConnection connection = new SqlConnection();
public string sorgu;
public static string server;
public static string userId;
public static string catalog;
public static string password;
public static string accessMethod;
public DataSet ds = new DataSet();
Form progressForm = new Form();
public bool Open()
{
try
{
if (connection.State != ConnectionState.Open)
{
connection.ConnectionString = "Data Source = " + server + ";" +
"Initial Catalog=" + catalog + ";" +
"User ID=" + userId + ";" +
"Password=" + password + ";" +
"Connect Timeout=0";
connection.Open();
return true;
}
else
{
return true;
}
}
catch (Exception ex)
{
MessageBox.Show("System message:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public DataTable Dt(string query)
{
DataTable dt = new DataTable();
if (Open())
{
SqlDataAdapter da = new SqlDataAdapter(query, connection);
try
{
//progressForm.Showdialog() is this possible???
da.Fill(dt);
//progressForm.close(); ??
}
catch (Exception ex)
{
MessageBox.Show("Sistem Mesajı:" + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return dt;
}
public bool Run(string query, string hataMsj)
{
Form activeForm = Form.ActiveForm;
query = " SET DATEFORMAT DMY " + query;
SqlCommand sc = new SqlCommand(query, connection);
try
{
Open();
sc.ExecuteNonQuery();
return true;
}
catch (Exception )
{
return false;
}
}
public void fillComboBox(string sorgu, ComboBox cb, string text, string value)
{
DataTable dt = Dt(sorgu);
cb.DisplayMember = text;
cb.ValueMember = value;
cb.DataSource = dt;
if (cb.Items.Count > 0)
{
cb.SelectedIndex = 0;
}
}
public int fillGridView(string sorgu, DataGridView dgv)
{
DataTable dtGrvw = Dt(sorgu);
dgv.DataSource = dtGrvw;
return 1;
}
}
and example queries from another form(class)
来自另一个表单(类)的示例查询
ConnectionClass cc = new ConnectionClass();
query= " INSERT INTO tblPersonel (" +
" [sqlUserName] " +
",[personelNo] " +
",[ad] " +
",[soyad] " +
",[departmanId] " +
",[emailadres] " +
",[tcKimlikNo],[kangurubu],[dokumaciNo])VALUES" +
"('" + tbSqlUserName.Text +
"','" + tbPersonelNo.Text +
"','" + tbAd.Text +
"','" + tbSoyad.Text +
"','" + cbDepartman.SelectedValue.ToString() +
"','" + tbMail.Text +
"','" + tbKimlikno.Text +
"','" + tbKangrubu.Text +
"','" + tbDokumaciNo.Text + "' ) ";
if (cc.Run(query, "Unexpected error on insert new person"))
{
fillGrid();
this.Close();
}
public void fillGrid()
{
query= " select * from View_Personel order by personelNo desc";
cc.fillGridView(query, gridviewPersonel);
}
and I cant imagine how can I use it in bw_DoWork event. because my function has parameters.(query, gridview) when I call it from another class I can use it with parameters...
我无法想象如何在bw_DoWork事件中使用它。因为我的函数有参数。(查询,gridview)当我从另一个类调用它时我可以使用参数...
p.s. : this Method is pretty good for me but it didnt worked. I didnt understand the problem
附: :这个方法对我来说非常好,但它没有用。我不明白这个问题
2 个解决方案
#1
4
Use the BackgroundWorker class to fill your DataGrid.
使用BackgroundWorker类填充DataGrid。
Form progressForm;
public void func() {
BackgroundWorker bw = new BackgroundWorker ();
bw.DoWork += new DoWorkEventHandler (bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler (bw_RunWorkerCompleted);
progressForm = new Form ();
ProgressBar pb = new ProgressBar ();
pb.MarqueeAnimationSpeed = 30;
pb.Style = ProgressBarStyle.Marquee;
pb.Dock = DockStyle.Fill;
progressForm.ClientSize = new Size (200, 50);
progressForm.FormBorderStyle = FormBorderStyle.FixedDialog;
progressForm.StartPosition = FormStartPosition.CenterScreen;
progressForm.Controls.Add (pb);
progressForm.ControlBox = false;
progressForm.TopMost = true;
progressForm.Show ();
string queryString = "SELECT ...."; // fill query string here
var params = new KeyValuePair<GridControl, string>(sorgu, queryString);
bw.RunWorkerAsync (params);
}
void bw_DoWork (object sender, DoWorkEventArgs e) {
KeyValuePair<GridControl, string> params = e.Argument as KeyValuePair<GridControl, string>;
ConnectionClass cc = new Connection Class();
cc.fillGrid(params.Value, params.Key);
}
void bw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e) {
progressForm.Close (); //
}
It is possible to send a parameter to the BackgroundWorker. If you need more than one parameter, you can send a Tuple which contains any objects you need.
可以将参数发送到BackgroundWorker。如果需要多个参数,可以发送包含所需对象的元组。
EDIT: If you're on 3.5, you can use a KeyValuePair instead. Code is updated for that.
编辑:如果你在3.5,你可以使用KeyValuePair。代码已更新。
#2
0
Just as Ash Burlaczenko recommended, you'll have to use a BackgroundWorker for that purpose.
就像Ash Burlaczenko推荐的那样,你必须为此目的使用BackgroundWorker。
Since, however, you'd like to tie it in with a ProgressBar, I'd recommend looking at this article on CodeProject: ProgressWorker.
但是,由于您希望将其与ProgressBar绑定,我建议您查看CodeProject上的这篇文章:ProgressWorker。
It's fairly easy to use and it updates the progress bar for you automatically. All you'll have to do is remember to call the ProgressWorker.ReportProgress
method from time to time in order to update the associated progress bar.
它相当容易使用,它会自动为您更新进度条。您所要做的就是记住不时调用ProgressWorker.ReportProgress方法以更新相关的进度条。
#1
4
Use the BackgroundWorker class to fill your DataGrid.
使用BackgroundWorker类填充DataGrid。
Form progressForm;
public void func() {
BackgroundWorker bw = new BackgroundWorker ();
bw.DoWork += new DoWorkEventHandler (bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler (bw_RunWorkerCompleted);
progressForm = new Form ();
ProgressBar pb = new ProgressBar ();
pb.MarqueeAnimationSpeed = 30;
pb.Style = ProgressBarStyle.Marquee;
pb.Dock = DockStyle.Fill;
progressForm.ClientSize = new Size (200, 50);
progressForm.FormBorderStyle = FormBorderStyle.FixedDialog;
progressForm.StartPosition = FormStartPosition.CenterScreen;
progressForm.Controls.Add (pb);
progressForm.ControlBox = false;
progressForm.TopMost = true;
progressForm.Show ();
string queryString = "SELECT ...."; // fill query string here
var params = new KeyValuePair<GridControl, string>(sorgu, queryString);
bw.RunWorkerAsync (params);
}
void bw_DoWork (object sender, DoWorkEventArgs e) {
KeyValuePair<GridControl, string> params = e.Argument as KeyValuePair<GridControl, string>;
ConnectionClass cc = new Connection Class();
cc.fillGrid(params.Value, params.Key);
}
void bw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e) {
progressForm.Close (); //
}
It is possible to send a parameter to the BackgroundWorker. If you need more than one parameter, you can send a Tuple which contains any objects you need.
可以将参数发送到BackgroundWorker。如果需要多个参数,可以发送包含所需对象的元组。
EDIT: If you're on 3.5, you can use a KeyValuePair instead. Code is updated for that.
编辑:如果你在3.5,你可以使用KeyValuePair。代码已更新。
#2
0
Just as Ash Burlaczenko recommended, you'll have to use a BackgroundWorker for that purpose.
就像Ash Burlaczenko推荐的那样,你必须为此目的使用BackgroundWorker。
Since, however, you'd like to tie it in with a ProgressBar, I'd recommend looking at this article on CodeProject: ProgressWorker.
但是,由于您希望将其与ProgressBar绑定,我建议您查看CodeProject上的这篇文章:ProgressWorker。
It's fairly easy to use and it updates the progress bar for you automatically. All you'll have to do is remember to call the ProgressWorker.ReportProgress
method from time to time in order to update the associated progress bar.
它相当容易使用,它会自动为您更新进度条。您所要做的就是记住不时调用ProgressWorker.ReportProgress方法以更新相关的进度条。