【问题标题】:Missing DataGridview scrollbar C# WinForm缺少 DataGridview 滚动条 C# WinForm
【发布时间】:2014-03-26 04:45:07
【问题描述】:

我有一个带有 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 propertiestrue)并自定义列长。

数据仍然可以通过鼠标滚动。它是如何发生的?我该如何解决?

这是我的程序的预览:link

【问题讨论】:

    标签: c# winforms datagridview text-files freeze


    【解决方案1】:

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

    我移动了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.Datasource = NULL”并在之后设置它来修复:“datagridview.Datasource = bindingsource”
    【解决方案2】:

    如果滚动条丢失,添加类似这样的内容以向 datagridview 添加滚动条

    InputDataGridView.ScrollBars == Windows.Forms.ScrollBars.Both
    'or
    InputDataGridView.ScrollBars == Windows.Forms.ScrollBars.Vertical
    

    【讨论】:

    • 我做了你的建议。但这并没有解决我的问题。那个地方有scrollbarr的痕迹,但是颜色是白色的。该过程的工作时间也比我从 Visual Studio 运行时长。
    • 这是我的程序预览(点击链接):link
    【解决方案3】:
    if (productsDataGridView.InvokeRequired) { 
        productsDataGridView.Invoke(new MethodInvoker(delegate { LoadInputData() })); 
    }
    

    【讨论】:

    • 虽然这个答案可能是正确的,但您应该通过解释此代码的作用以及它如何解决原始问题来提高答案的质量——这有助于阅读您的答案的其他用户了解什么正在进行中。
    【解决方案4】:

    在调用禁用单元格方法后简单添加此行

    myDataGridView.ScrollBars = ScrollBars.Both;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多