在向数据表添加行时实现进度条
本文关键字:实现 数据表 添加行 | 更新日期: 2023-09-27 18:15:24
我写了一个代码来传输值在一个文本文件到一个数据表。由于记录的数量很高,我实现了一个进度条来查看状态。但是它会抛出一个名为
的错误"值'1'对'Value'无效。"Value"应该介于两者之间"最小"answers"最大"。"
你能帮我在循环内成功实现进度条吗?下面是我使用的代码:
我已经更新了我的编码与后台工作人员。但是它会抛出一个名为:
的错误"创建窗口句柄错误"
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog thisDialog = new OpenFileDialog();
DataTable dt = new DataTable();
DataRow dr = null;
if (thisDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text=thisDialog.FileName;
string file1 = textBox1.Text;
//background worker
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
backgroundWorker1.CancelAsync();
}
using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
{
string line=String.Empty;
int lineno = 0;
while ((line = file.ReadLine()) != null)
{
if (line.Contains("DISKXFER"))
{
string dataLine = line.ToString();
string[] split = dataLine.Split(',');
int result = split.Length;
if (lineno == 0)
{
for (int x = 0; x < result; x++)
{
DataColumn dcss = new DataColumn(x.ToString(), Type.GetType("System.String"));
dt.Columns.Add(dcss);
}
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
for (int x = 0; x < result; x++)
{
dr[x+1] = split[x];
}
}
else
{
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
for (int x = 0; x < result; x++)
{
dr[x+1] = split[x];
}
}
lineno += 1;
}
}
}
}
dataGridView1.DataSource = dt;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string line;
string file1 = textBox1.Text;
using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
{
int count = 0;
while ((line = file.ReadLine()) != null)
{
if (line.Contains("DISKXFER"))
{
backgroundWorker1.ReportProgress(count);
}
count += 1;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
如果你想让你的应用程序在循环行时不处于"不响应"状态,那么你可以使用后台worker。但是要小心。文件对话框必须在线程之外,以免出现任何跨线程异常!将文件路径保存到一个私有字段,并在后台工作器中使用它。每次您存储一行,然后使用ReportProgress来更新进度条!!举一个使用Background Worker的例子:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
private BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
if (bw.WorkerSupportsCancellation == true)
{
bw.CancelAsync();
}
private void bw_DoWork(object sender, DoWorkEventArgs e){ }
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){ }
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e){ }
编辑:private string file1;
private DataTable dt;
private int iTotalLinesOfFile;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog thisDialog = new OpenFileDialog();
if (thisDialog.ShowDialog() == DialogResult.OK)
{
if(dt==null)
{
dt== new DataTable();
}
else
{
dt.Clear();
}
file1 = thisDialog.FileName;
textBox1.Text=file1;
iTotalLinesOfFile = System.IO.File.ReadAllLines(file1).Length;
//background worker
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync(); //when you call this the DoWork will start and the code will continue!!
}
//do not let the user to click the button again!!
button1.Enabled = false;
//you will use this code if you want to cancel the job that Worker does.
//if (backgroundWorker1.WorkerSupportsCancellation == true)
//{
// backgroundWorker1.CancelAsync();
//}
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
DataRow dr = null;
using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
{
string line=String.Empty;
int lineno = 0;
int count = 0;
while ((line = file.ReadLine()) != null)
{
if (line.Contains("DISKXFER"))
{
backgroundWorker1.ReportProgress(count);
string dataLine = line.ToString();
string[] split = dataLine.Split(',');
int result = split.Length;
if (lineno == 0)
{
for (int x = 0; x < result; x++)
{
DataColumn dcss = new DataColumn(x.ToString(), Type.GetType("System.String"));
dt.Columns.Add(dcss);
}
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
for (int x = 0; x < result; x++)
{
dr[x+1] = split[x];
}
}
else
{
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
for (int x = 0; x < result; x++)
{
dr[x+1] = split[x];
}
}
lineno += 1;
}
count += 1;
}
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
dataGridView1.DataSource = dt;
button1.Enabled = true;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int iCount = e.ProgressPercentage;
if(iTotalLinesOfFile==0) return;
//progressBar1.Value must not be less than 0 and more than 100
progressBar1.Value = (iCount / iTotalLinesOfFile) * 100;
}
progressBar1。目前最大值被设置为零!
-
选项1:定义progressBar1的最大值
progressBar1。
-
选项2:使用Endlessprogressbar来达到你的目标
编辑:包含选项1的更正代码:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog thisDialog = new OpenFileDialog();
DataTable dt = new DataTable();
DataRow dr = null;
progressBar1.Minimum = 0;
progressBar1.Step = 1;
progressBar1.Visible = true;
if (thisDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text=thisDialog.FileName;
string file1 = textBox1.Text;
progressBar1.Maximum = File.ReadAllLines(file1).Length;
using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
{
string line=String.Empty;
int lineno = 0;
while ((line = file.ReadLine()) != null)
{
if (line.Contains("DISKXFER"))
{
string dataLine = line.ToString();
string[] split = dataLine.Split(',');
int result = split.Length;
if (lineno == 0)
{
for (int x = 0; x < result; x++)
{
DataColumn dcss = new DataColumn(x.ToString(), Type.GetType("System.String"));
dt.Columns.Add(dcss);
}
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
for (int x = 0; x < result; x++)
{
dr[x+1] = split[x];
}
}
else
{
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
for (int x = 0; x < result; x++)
{
dr[x+1] = split[x];
}
}
progressBar1.Value = dt.Rows.IndexOf(dr);
Application.DoEvents();
lineno += 1;
}
}
}
}
dataGridView1.DataSource = dt;
}
正如其他人已经指出的那样:后台工作人员会是一个更好的选择。特别是当你的UI仍然需要响应时。
简单地说,当您分配此值时,数据表中没有行,因此最大值为0。
progressBar1.Maximum = dt.Rows.Count;
无论如何,由于您似乎不知道提前的行数,因此显示任何"进度"是没有任何意义的,因为当您扩展最大值时,它总是满的。不过你也可以使用选框模式。或者预先用将要添加的行数初始化最大值。
您应该设置您想要的ProgressBar.Maximum
和ProgressBar.Minimum
属性范围。
此属性获取或设置控件范围的最大值和最大值。默认为0
和100
。
For Minimum
property;
此属性指定Value属性的下限。当最小属性的值被更改,即ProgressBar控件重新绘制以反映控件的新范围。的值值属性等于最小属性的值进度条为空。要更改进度条的值,请使用Step属性与PerformStep方法,使用增量方法,或直接设置value属性的值