0
votes

I'm currently seeing the following problem: I have a DataGridView that is filled by a Task running in the background. After the task is finished, the scrollbars and cells aren't shown properly in the DataGridView somehow... After resizing the dialog to fullscreen mode (maximized), the scrollbars are shown properly... Whenever the resizing is done the other way (to minimized), the scrollbar fails again... Any ideas here? Is there any refresh event I can trigger from the Task to readjust the scrollbars and cells?

**Additional information: ** The DataGridView is packed onto a TableLayoutPanel with Dock = Fill.

enter image description here enter image description here

Edit: The data loading is done via

private void TryLoadData()  
{
    try
    {
        LoadData();
    }
    catch (Exception ex)
    {
        //Just some error logging
        _log.Error(ex);
        _errorHandler.Show(ex);
    }
}

private void LoadData()
{
    ClearRows();
    //Loading from database
    var data = _databaseAdapter.Get<Data, bool>(x => !x.Deleted);
    foreach (var singleDatum in data)
        LoadDataRowToDataGridView (singleDatum);
}

private void ClearRows()
{
    this.UiThreadInvoke(() => { DataGridView.Rows.Clear(); });
}

private void LoadDataRowToDataGridView(Data singleDatum)
{
    this.UiThreadInvoke(() => { DataGridView.Rows.Add(singleDatum.Id, singleDatum.Name); });
}

Started via:

new Task(TryLoadData).Start();

UiThreadInvoke:

using System;
using System.Windows.Forms;

namespace UIExtensions
{
    public static class UiThreadInvokeExtension
    {
        public static void UiThread(this Control control, Action code)
        {
            if (control.InvokeRequired)
            {
                control.BeginInvoke(code);
                return;
            }
            code.Invoke();
        }

        public static void UiThreadInvoke(this Control control, Action code)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(code);
                return;
            }
            code.Invoke();
        }
    }
}
2
I had exactly the same problem, but can't remember how i solved that grrrr. Can you add code snippet with example how are you inserting data to datagridview in backgroundworker ?kocica
[stackoverflow.com/users/8254699/] (@Filip Kočica) Code snippets are there ;)FranzHuber23
Hmm you are doing it same as me, almost. Try to set properties which i've written in comment below :-)kocica
Yeah, I missed the property to autosize the columns... But it's strange that the scrollbar is not shown anyways...FranzHuber23
OK ill take a look at my code tomorrow and try to find out where could be the problem.kocica

2 Answers

2
votes

Try these

1. Set these properties

ContactsDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
ContactsDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;

2. Insert in like this

ContactsDataGridView.Invoke(new Action(() =>
{
    ContactsDataGridView.Rows.Add(kvp.Key, kvp.Value, new DataGridViewButtonCell(), new DataGridViewButtonCell());
}));
0
votes

when you resize the screen the app re draws and renders the window. try to force the rendering of the grid when the data is loaded.

datagridview1.update(); datagridview1.refresh();