dotnet/winforms

DataGridView unshare rows in VirtualMode despite all conditions to prevent it are met.

Open

#6,930 opened on Mar 29, 2022

View on GitHub
 (5 comments) (0 reactions) (1 assignee)C# (922 forks)batch import
area-controls-DataGridViewhelp wanted

Repository metrics

Stars
 (4,100 stars)
PR merge metrics
 (Avg merge 14d 22h) (56 merged PRs in 30d)

Description

.NET version

6.0.201

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Tested with .NET Framework 4.6.1, same issue.

Issue description

When creating DataGridView and setting VirtualMode to true it is not possible to prevent rows from becoming unshared.

As far as i see, i did everything that is described in this article to prevent rows becoming unshared, but no luck. This article is not updated for .NET Core or later.

Please take a look at the video, rows becoming unshared on row (cell) select (in 4.6.1 same behavior) but in 6.0.2 it also for some reason incrementally unshare all rows from 0 to the end in inconsistent manner (sometimes it won't start it unless row changed with arrow keys or some other woodoo)

https://user-images.githubusercontent.com/102589197/160618007-a6077a18-8869-4388-9cbd-52ea604f4fec.mp4

Steps to reproduce

Code below is not exactly what i'm trying to achieve, it is a result of experiments to see if i can solve this issue. My goal is to display large set of data using virtual mode without unsharing rows.

namespace WinFormsApp1;

internal static class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new Form1());
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        grid.ReadOnly = true;
        grid.AllowUserToResizeColumns = false;
        grid.AllowUserToResizeRows = false;
        grid.ShowCellToolTips = false;
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "One", Name = "c_one1" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Two", Name = "c_one2" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Three", Name = "c_one3" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Four", Name = "c_one4" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Five", Name = "c_one5" });
        grid.VirtualMode = true;
        grid.EnableHeadersVisualStyles = false;
        grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        grid.RowHeadersVisible = false;
        grid.MultiSelect = false;
        grid.ColumnHeadersVisible = false;
        grid.RowUnshared += Grid_RowUnshared;
        grid.CellValueNeeded += Grid_CellValueNeeded;
        grid.RowCount = 1000000;
    }

    private void Grid_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        e.Value = e.RowIndex.ToString();
    }

    private void Grid_RowUnshared(object sender, DataGridViewRowEventArgs e)
    {
        tb.AppendText($"Row {e.Row.Index} unshared" + Environment.NewLine);
    }

    #region Designer
    private System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
            components.Dispose();
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.grid = new System.Windows.Forms.DataGridView();
        ((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
        this.SuspendLayout();
        // 
        // grid
        // 
        this.grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.grid.Location = new System.Drawing.Point(22, 12);
        this.grid.Name = "grid";
        this.grid.RowTemplate.Height = 25;
        this.grid.Size = new System.Drawing.Size(534, 412);
        this.grid.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.grid);
        this.Name = "Form1";
        this.Text = "Form1";
        //textbox
        tb = new TextBox();
        tb.Size = new Size(200, 412);
        tb.Location = new Point(22 + 536, 12);
        tb.Multiline = true;
        Controls.Add(tb);

        ((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
        this.ResumeLayout(false);
    }

    private DataGridView grid;
    private TextBox tb;
    #endregion    
}

Contributor guide