C# DataGridView doesn't show Scrollbars when filling from Task

3.4k views Asked by At

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

There are 3 answers

2
kocica On BEST ANSWER

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

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

0
D J On

I found this looking for a fix for my similar issue where I was loading the grid on a TabPage from a background thread that would set the grid's data-source via a grid.Invoke. If the load occurred when the page was not already showing, the scrollbars did not show up when I then selected that page until I minimized/maximized my form. For me, I was able to resolve this by subscribing to the TabPage Enter event; I then called SuspendLayout() followed by ResumeLayout() and the scrollbars appeared. I found this by accident while trying various other things to remedy it. Refresh() and Invalidate() did not work for me.