[英]Missing DataGridview scrollbar C# WinForm
我有一個帶有 datagridview 的 WindowsForm 解決方案來顯示我從文本文件中讀取的數據。 來自數據的行數很大,大約 10.000 行。
當我從 Visual Studio 運行程序時,它看起來很好。 但是當我從 Debug 文件夾(.exe 文件)運行它時,我的 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
)並自定義列長。
數據仍然可以通過鼠標滾動。 它是如何發生的? 我該如何解決這個問題?
這是我的程序的預覽:鏈接
我解決了這些問題。 它是由后台工作人員引起的。 我不知道如何從技術上解釋這個概念。 但是,我做到了。
我移動了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);
}
}
如果缺少滾動條,請添加類似的內容以將滾動條添加到 datagridview
InputDataGridView.ScrollBars == Windows.Forms.ScrollBars.Both
'or
InputDataGridView.ScrollBars == Windows.Forms.ScrollBars.Vertical
if (productsDataGridView.InvokeRequired) {
productsDataGridView.Invoke(new MethodInvoker(delegate { LoadInputData() }));
}
在調用禁用單元格方法后簡單添加此行
myDataGridView.ScrollBars = ScrollBars.Both;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.