C# 在向datatable添加行时实现进度条
我编写了一个代码,将文本文件中的值传输到数据表。由于记录的数量很多,我实现了一个进度条来查看状态。但它抛出一个错误,称为 “1”的值对“值”无效。“值”应介于 “最小”和“最大” 你能帮我在循环中成功地实现进度条吗。以下是我使用的代码: 我已经用backgroundworker更新了我的编码。但它会抛出一个错误,称为: “创建窗口句柄时出错”C# 在向datatable添加行时实现进度条,c#,C#,我编写了一个代码,将文本文件中的值传输到数据表。由于记录的数量很多,我实现了一个进度条来查看状态。但它抛出一个错误,称为 “1”的值对“值”无效。“值”应介于 “最小”和“最大” 你能帮我在循环中成功地实现进度条吗。以下是我使用的代码: 我已经用backgroundworker更新了我的编码。但它会抛出一个错误,称为: “创建窗口句柄时出错” private void按钮1\u单击(对象发送者,事件参数e) { OpenFileDialog thisDialog=新建Op
private void按钮1\u单击(对象发送者,事件参数e)
{
OpenFileDialog thisDialog=新建OpenFileDialog();
DataTable dt=新的DataTable();
数据行dr=null;
if(thisDialog.ShowDialog()==DialogResult.OK)
{
textBox1.Text=thisDialog.FileName;
字符串file1=textBox1.Text;
//背景工作者
backgroundWorker1.DoWork+=新的DoWorkerVenthandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged+=新的ProgressChangedEventHandler(backgroundWorker1\u ProgressChanged);
backgroundWorker1.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(backgroundWorker1\U RunWorkerCompleted);
if(backgroundWorker1.IsBusy!=true)
{
backgroundWorker1.RunWorkerAsync();
}
if(backgroundWorker1.WorkerSupportsScanCellation==true)
{
backgroundWorker1.CancelAsync();
}
使用(System.IO.StreamReader文件=新的System.IO.StreamReader(文件1))
{
string line=string.Empty;
int lineno=0;
而((line=file.ReadLine())!=null)
{
if(行包含(“DISKXFER”))
{
字符串dataLine=line.ToString();
string[]split=dataLine.split(',');
int结果=分割长度;
如果(行号==0)
{
for(int x=0;x 如果(dt.Rows.CountprogressBar1.max当前设置为零!
- 选项1:定义progressBar1的最大值
progressBar1.max=File.ReadAllLines(“File.txt”).Length
- 选项2:用于实现您的目标
编辑:包括选项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;
}
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;
}
private void按钮1\u单击(对象发送者,事件参数e)
{
OpenFileDialog thisDialog=新建OpenFileDialog();
DataTable dt=新的DataTable();
数据行dr=null;
progressBar1.最小值=0;
progressBar1.步骤=1;
progressBar1.Visible=true;
if(thisDialog.ShowDialog()==DialogResult.OK)
{
textBox1.Text=thisDialog.FileName;
字符串file1=textBox1.Text;
progressBar1.Maximum=File.ReadAllLines(file1).Length;
使用(System.IO.StreamReader文件=新的System.IO.StreamReader(文件1))
{
string line=string.Empty;
int lineno=0;
而((line=file.ReadLine())!=null)
{
if(行包含(“DISKXFER”))
{
字符串dataLine=line.ToString();
string[]split=dataLine.split(',');
int结果=分割长度;
如果(行号==0)
{
for(int x=0;x 如果简单地(dt.Rows.Count,则在分配此值时,datatable中没有行,因此最大值为0
progressBar1.Maximum = dt.Rows.Count;
无论如何,由于您似乎事先不知道行数,因此显示任何“进度”都没有任何意义因为当您扩展最大值时,它总是满的。但是,您可以使用选框模式。或者使用要添加的行数预先初始化最大值。您应该设置属性和所需的范围
此属性获取或设置控件范围的最大值和最大值。它们的默认值为0
和100
对于最小值
属性
此属性指定Value属性的下限。当
最小属性的值已更改,ProgressBar控件
重新绘制以反映控件的新范围。当
Value属性等于最小属性的值
进度条为空。若要更改进度条的值,请使用
对于PerformStep方法的步骤属性,请使用增量
方法,或直接设置value属性的值
如果希望应用程序不处于“无响应”状态在行中循环时声明,然后可以使用后台工作程序。但请小心。文件对话框必须位于线程之外,以免出现任何跨线程异常!将文件路径保存到专用字段,并在后台工作程序中使用它。每次存储行时,请使用ReportProgress更新进度栏!!
以使用后台工作程序为例:
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;
}