缺少DataGridview滚动条c# WinForm

本文关键字:WinForm 滚动条 DataGridview 缺少 | 更新日期: 2023-09-27 17:53:51

我有一个datagridview的WindowsForm解决方案来显示我从文本文件中读取的数据。来自数据的行数很大,大约有10,000行。

当我从visual studio运行程序时,它看起来很好。但是当我从调试文件夹(.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 propertiestrue)和自定义列长度。

数据仍然可以用鼠标滚动。这是怎么发生的?怎么解呢?

这是我的程序预览:link

缺少DataGridview滚动条c# WinForm

我解决了这些问题。这是由后台工人引起的。我不知道如何从技术上解释这个概念。但是,在这里我做到了。

我移动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);
        }
    }

To this place:

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() })); 
}

simple在调用disable cells方法后添加这一行

myDataGridView.ScrollBars = ScrollBars.Both;