2

I have implemented a DataGridView on my form and successfully implemented VirtualMode. This retrieves cell data from a local cache and all appears to be working correctly when populating the grid/paging etc. I handle the DataGridView.CellValueNeeded event to populate the cells.

On the DataGridView, I have the AutoSizeColumnsMode property set to DataGridViewAutoSizeColumnsMode.DisplayedCells. I have noticed that in using VirtualMode the DataGridView does not appear to respect the AutoSizeColumnsMode after populating the cells. I have examined this article but not found a solution.

What I would ultimately like to do is to not rely on the AutoSizeColumnsMode property, but rather call the .AutoResizeColumn() method somewhere to resize, so I initially autosize the column, but then allow the user to resize.

I have tried the following with limited or no success:

  1. Set DataGridView.AutoSizeColumnsMode to .None. Then in my .CellValueNeeded handler

    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        // ... Get cell value from cache
       dataGridView.AutoResizeColumn(e.ColumnIndex, DataGridViewAutoSizeColumnMode.DisplayedCells);
     }
    

    This throws a StackOverFlowException presumably because it repeatedly raises .CellValueNeeded.

  2. Tried exactly the same thing except in the .CellFormatting event handler. Got the same StackOverFlowException.

  3. Tried with and without DataGridView.SuspendLayout/ResumeLayout:

    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        // ... Get cell value from cache
       dataGridView.CellValueNeeded -= dataGridView_CellValueNeeded;
       dataGridView.AutoResizeColumn(e.ColumnIndex, DataGridViewAutoSizeColumnMode.DisplayedCells);
       dataGridView.CellValueNeeded += dataGridView_CellValueNeeded;
    }
    

    This gives all blank cells, so no use.

  4. This one actually somewhat works, for a reason I don't understand:

    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        // ... Get cell value from cache
        dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
    }
    

    It resizes the columns correctly, but it seems odd to have to repeatedly call it on every cell value needed. Also, I cannot immediately set it to .None right after or it will StackOverFlowException again. Thus, I cannot allow user to resize columns.

  5. Calling .UpdateCellValue() as mentioned in the article from my .CellValueNeeded handler throws StackOverFlowException as well.

So is it possible to call .AutoResizeColumn() somewhere where it won't raise .CellValueNeeded until it overflows? Since #4 seems to have the ability to perform the autosize function it seems as though I could manually call it from somewhere as well.

2 Answers 2

2

I think this may be the solution, although I am still interested in hearing what others have to say.

I continued to look at some of the other events raised by the DataGridView and found the .RowPostPaint event. I created the following handler:

private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    if (dataGridView.AllowUserToResizeColumns) //So not to run unnecessarily
    {
        return;
    }
    var lastIndex = dataGridView.Rows.GetLastRow(DataGridViewElementStates.Displayed);
    if (e.RowIndex == lastIndex) //Only do on the last displayed row
    {
        dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
        dataGridView.AllowUserToResizeColumns = true;  // User has control from here on
    }
}

This accomplishes the autosizing of the columns upon the initial data-load, then allows the user to re-size from there. It only does this once, so way better than on every cell value needed. I must set dataGridView.AllowUserToResizeColumns = false prior to initial data loading.

This seems to fit the bill. The user will see nicely fit columns upon initial load and can tweak from there, and most of my data is comparable length from row to row, so should not truncate or waste space after that in most cases.

0

Since you state that you are interested in what others have to say, I've put together a slightly different approach for how to use automatic column resizing with a virtual data grid.

The initial AutoResizeColumns call is placed after the Shown event so that the form and child components are initialized and displayed. Also, by wiring the DataGridView Scroll event for resizing, rather than RowPostPaint, this should be ever so slightly more efficient because this event fires less frequently, and I think it follows along nicely with the MSDN reference you cite:

using System.Collections.Generic;
using System.Windows.Forms;

namespace DataGridViewTest
{
    public partial class DataGridViewForm : Form
    {
        private List<string> dataSource;

        public DataGridViewForm()
        {
            InitializeComponent();

            // Enable VirtualMode for dataGridView1
            dataGridView1.VirtualMode = true;

            // Wire CellValueNeeded event handler
            dataGridView1.CellValueNeeded += DataGridView1_CellValueNeeded;

            // Wire Scroll event handler
            dataGridView1.Scroll += DataGridView1_Scroll;

            // Wire form Shown event handler
            this.Shown += DataGridViewForm_Shown;
        }

        private void DataGridViewForm_Shown(object sender, System.EventArgs e)
        {
            // Populate dataGridView1 here to avoid perception of a long form startup time
            populateDataGridView();

            // Resize columns after the form is initialized and displayed on screen,
            // otherwise calling this method won't actually have an effect on column sizes
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
        }

        private void DataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            // Set the triggering cell's value to the corresponding value from dataSource
            e.Value = dataSource[e.RowIndex];
        }

        private void DataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            // Resize columns again, but only if a vertical scroll just happened
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
            }
        }

        private void populateDataGridView()
        {
            fetchIntoDataSource();

            associateDataSourceToDataGridView();
        }

        private void fetchIntoDataSource()
        {
            dataSource = new List<string>();

            // Insert a test string into dataSource many times
            for (int i = 0; i < 1000; i++)
            {
                dataSource.Add("test string");
            }
        }

        private void associateDataSourceToDataGridView()
        {
            // Synchronize dataGridView1.RowCount to dataSource.Count
            // This is necessary for the CellValueNeeded event to fire
            dataGridView1.RowCount = dataSource.Count;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.