简体   繁体   中英

Missing DataGridview scrollbar C# WinForm

I have WindowsForm solution with a datagridview to display the data that I read from text file. The number of rows from data is large, about 10.000 lines.

When I run the program from visual studio, it seems fine. But when I run it from the Debug folder (.exe file), then something goes wrong with my 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. The DisableCells() function is to lock the datagridview (ie set the readonly properties be true ) and customize the column length.

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(); line. Previously, I put it inside private void OpenDataBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) function. 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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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