我有一个带datagridview的WindowsForm解决方案,可以显示我从文本文件中读取的数据。数据的行数很大,大约10.000行。
当我从Visual Studio运行该程序时,看起来还不错。但是,当我从Debug文件夹(.exe文件)运行它时,我的datagridview出了点问题。滚动条丢失。
这是我填充datagridview的方法:
private void LoadInputData()
{
try
{
InputDataGridView.DataSource = null;
InputDataGridView.Refresh();
InputDataGridView.DataSource = inputDataTable;
DisableCells();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Load Input Data Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我有一个旨在填充文本文件中的
inputDataTable
的函数。 DisableCells()
函数用于锁定datagridview(即,将readonly properties
设置为true
)并自定义列长度。数据仍然可以通过鼠标滚动。它是怎么发生的?我该如何解决?
这是我的程序的预览:link
最佳答案
我解决了问题。这是由背景工作者造成的。我不知道如何从技术上解释这个概念。但是,我在这里做了。
我移动LoadInputData();
行。以前,我将其放在private void OpenDataBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
函数中。然后,我将其移至后台工作者之外的另一个地方。可以在下面的代码中看到。
先前:
(请参阅“ //”)
private void OpenDataBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
//LoadInputData();
CalculateRowAndColumnInNumericUpDown();
mainForm.MainToolStripProgressBar.Value = 0;
this.Cursor = Cursors.Default;
OpenDataButton.Enabled = true;
ProcessGroupBox.Enabled = true;
ClearAllDataButton.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Open Data Background Worker RunWorkerCompleted Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
到这个地方:
private void OpenDataButton_Click(object sender, EventArgs e)
{
try
{
OpenDataButton.Enabled = false;
if (!OpenDataBackgroundWorker.IsBusy)
{
OpenFileDialog openData = new OpenFileDialog();
openData.Multiselect = true;
openData.ShowDialog();
openData.Filter = "allfiles|*";
if (openData.FileName != "")
{
ClearInputDataTable();
LoadInputData();
OpenDataBackgroundWorker.WorkerReportsProgress = true;
OpenDataBackgroundWorker.WorkerSupportsCancellation = true;
OpenDataBackgroundWorker.RunWorkerAsync(openData.FileName);
}
}
//here!!!
LoadInputData();
OpenDataButton.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error - Open Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}