简体   繁体   English

缺少 DataGridview 滚动条 C# WinForm

[英]Missing DataGridview scrollbar C# WinForm

I have WindowsForm solution with a datagridview to display the data that I read from text file.我有一个带有 datagridview 的 WindowsForm 解决方案来显示我从文本文件中读取的数据。 The number of rows from data is large, about 10.000 lines.来自数据的行数很大,大约 10.000 行。

When I run the program from visual studio, it seems fine.当我从 Visual Studio 运行程序时,它看起来很好。 But when I run it from the Debug folder (.exe file), then something goes wrong with my datagridview.但是当我从 Debug 文件夹(.exe 文件)运行它时,我的 datagridview 出现了问题。 The scroll-bar is missing.滚动条不见了。

Here is how I fill the 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);
        }
    }

I have a function that is designed to fill the inputDataTable from a text file.我有一个函数,旨在从文本文件中填充inputDataTable The DisableCells() function is to lock the datagridview (ie set the readonly properties be true ) and customize the column length. DisableCells()函数是锁定datagridview(即设置readonly propertiestrue )并自定义列长。

The data still can be scrolled by mouse.数据仍然可以通过鼠标滚动。 How does it happen?它是如何发生的? How do I solve this?我该如何解决这个问题?

Here is preview of my program: link这是我的程序的预览:链接

I solved the problems.我解决了这些问题。 It caused by backgroundworker.它是由后台工作人员引起的。 I do not know how to explain the concept technicaly.我不知道如何从技术上解释这个概念。 But, here that I did.但是,我做到了。

I move the LoadInputData();我移动了LoadInputData(); line.线。 Previously, I put it inside private void OpenDataBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) function.以前,我把它放在private void OpenDataBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)函数中。 Then, I moved it to the another place outside the background worker.然后,我把它移到了后台工作者之外的另一个地方。 It can seen in this code below.它可以在下面的代码中看到。

Previously: (see the "//")以前:(见“//”)

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

add something like this to add scroll bars to the datagridview if the scroll bars are missing如果缺少滚动条,请添加类似的内容以将滚动条添加到 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM