私はExcelファイルから何千もの行を読み込んでDataGridViewに読み込むためにこのコードを書いています。BackgroundWorkerの問題
しかし、私が直面している問題は、どのファイルを読み込んでも、DataGridViewは最初のファイルのみから行を表示していて、_listは決してクリアされません。
public class MyForm : Form
{
private List<Student> _list = null;
private void LoadFile_Click(object sender, EventArgs e)
{
try
{
if (_list != null)
{
_list.Clear();
}
openFileDialog1.ShowDialog();
_connStr = MakeConnectionString.GetConnectionString(openFileDialog1.FileName);
if (!string.IsNullOrEmpty(_connStr))
{
backgroundWorker1.RunWorkerAsync();
}
}
catch
{
MessageBox.Show("Application is busy with the first task!", "Busy...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
IDataReader read = StudentDA.GetReader(_connStr);
List<Student> localList = null;
if (_list != null)
{
_list.Clear();
}
_list = StudentMapper.GetStudents(read);
localList = new List<Student>(_list);
dataGridView1.Invoke(new MethodInvoker(delegate
{
dataGridView1.Rows.Clear();
}));
foreach (Student std in localList)
{
dataGridView1.Invoke(new MethodInvoker(delegate
{
dataGridView1.Rows.Add(std.SerialNo, std.RollNo);
}));
}
}
}
リストをクリアするかどうかは、データバインディングではなく、b:次の行にリストを再割り当てするかどうかに関係ありません。私はまだ見ています... –
作業者が完了したイベントハンドラで_listをクリアします – TalentTuner